创建Spring MVC一般有以下几项内容
(1),创建动态Web项目,添加Spring,Spring MVC所需的jar包;
(2),在Web.xml中配置DispatcherServlet,加载Spring配置文件;
(3),编写处理请求的控制器(Controller);
(4),编写视图对象,这里使用JSP页面作为视图;
(5),配置Spring MVC的配置文件,使控制器,视图解析器等生效;
1,创建Web项目。
在/WEB-INF/lib文件中添加如下jar包
[Spring,Spring MVC所需jar包](http://download.csdn.net/download/weixin_38786831/9949843)
2,配置DispatcherServlet
在web.xml中配置DispatcherServlet,加载Spring配置文件,代码如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>SpringMvcFirst</display-name>
<!-- 监听Spring上下文容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 加载spring的XML配置文件到spring的上下文容器中 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置Spring MVC DispatcherServlet -->
<servlet>
<servlet-name>MVC</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<!-- 初始化方法 -->
<init-param>
<!-- 加载SpringMVC到spring的上下文容器中 -->
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/mvc*.*
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 配置DispatcherServlet所需要拦截的url -->
<servlet-mapping>
<servlet-name>MVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
3,编写控制器
编写一个名为UserController的控制器类,代码如下
package com.springmvc.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@RequestMapping("/login")
public String login(String username,String password,HttpServletRequest request){
System.out.println(username+" : " +password);
//将参数返回给页面
request.setAttribute("username", username);
request.setAttribute("password", password);
//指定要返回的页面为success.jsp
return "success";
}
}
4,编写视图
编写index.jsp页面,接收用户名和密码,代码如下
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>第一个Spring MVC实列</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<h2>第一个Spring MVC实列</h2><br>
<br>
<form action="login.do" method="post">
用户名:<input type="text" name="username"><br><br>
密 码:<input type="password" name="password"><br><br>
<input type="submit" value="提交" />
</form>
</body>
</html>
编写登录信息页面success.jsp,代码如下
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>success</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<h2>登录信息</h2><br>
用户名: ${username }<br>
密 码: ${password }
</body>
</html>
5,配置Spring MVC配置文件
在代码的根目录下创建Spring的配置文件applicationContext.xml,代码如下
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
</beans>
在代码的根目录下创建Spring MVC的配置文件mvc.context.xml,代码如下
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" >
<!-- 加载Spring的全局配置文件 -->
<beans:import resource="applicationContext.xml" />
<!-- Spring MVC配置 -->
<!-- 通过component-scan让Spring扫描org.swinglife.controller下的所有类,让Spring的代码注解失效 -->
<context:component-scan base-package="com.springmvc.controller">
</context:component-scan>
<!-- 配置Spring MVC的视图渲染器,让其前缀为:/后缀为。jsp将视图渲染到<method返回值>。jsp中 -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/" p:suffix=".jsp">
</beans:bean>
</beans:beans>
最后项目文件目录如下图