SpringMVC.入门篇.一.HelloWorld

时间:2023-03-09 07:19:28
SpringMVC.入门篇.一.HelloWorld

SpringMVC.入门篇《一》HelloWorld

项目包结构如下:

SpringMVC.入门篇.一.HelloWorld

HelloController.java 代码

 package com.charles.controller;

 import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; /**
* <p>Type: HelloController</p>
* <p>Description: Hello 控制层.</p>
* @author baitang.<gy03554>
* @date 2018年10月14日 下午3:44:57
* @version v1.0.0
*/
@Controller
public class HelloController { @RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello(Model model) { model.addAttribute("name", "公子缘");
return "hello";
}
}

hello-servlet.xml 配置文件如下:

 <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 扫描包 -->
<context:component-scan base-package="com.charles.controller" /> <!-- 这个类是从 spring-webmvc 这个jar包中copy过来的。 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean> </beans>

web.xml 配置文件如下:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5"> <display-name>springmvc</display-name> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <servlet>
<servlet-name>hello</servlet-name>
<!-- 前端控制器类,在spring-webmvc 这个jar包中,我是从这个包里面copy过来的。 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>

hello.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head> <body>
<h2>Hello, ${name}</h2>
</body>
</html>

index.jsp 代码

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head> <body>
<a href="hello">进入Hello页面.</a>
</body>
</html>

注意:

  load-on-startup : 表示启动容器时初始化该Servlet

  url-pattern: 表示哪些请求交给Spring Web MVC处理,“/”是用来定义默认servlet映射的。也可以填写" *.html "表示拦截所有以html为扩展名的请求。

  DispatcherServlet 默认使用WebApplicationContext作为上下文,Spring默认配置文件为“/WEB-INF/[servlet 名字]-servlet.xml”

运行项目,然后打开浏览器,输入访问地址:http://localhost:9826/springmvc

SpringMVC.入门篇.一.HelloWorld

点击:进入Hello页面 ,将会发起请求至后台控制层:HelloControl.java ,然后进入到:hello.jsp 页面。

SpringMVC.入门篇.一.HelloWorld

如有问题,欢迎纠正!!!

如有转载,请标明源处:https://www.cnblogs.com/Charles-Yuan/p/9786676.html