1 导入jar包
2 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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>springmvc</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!--springmvc 入口配置 start -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>加载/WEB-INF/目录下的所有XML作为springmvc-servlet.xml配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!--springmvc 入口配置 end -->
</web-app>
3 springmvc 配置文件 文件名已经在web.xml中指定为【springmvc-servlet.xml】,路径放在【/WEB-INF/springmvc-servlet.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--spring 组件扫描配置 -->
<context:component-scan base-package="com.qxb.mvc" />
<!-- 配置视图解析器 (该配置的是jstl解析器的一个扩展解析类) -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
注意:
<context:component-scan base-package="com.qxb.mvc" />这句话说明是基于注解的,扫描的就是你处理类所在的包我的是com.qxb.mvc
4 index.jsp 和login.jsp
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="test/123/qxb.do">欢迎进入springmvc的世界</a>
</body>
</html>
login.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>welcome</title>
</head>
<body>
${message}
</body>
</html>
5 处理类【基于注解】
package com.qxb.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
*
* 登录控制器
*
* @author qxb
*
* @date Feb 1, 2013 9:38:13 AM
*/
@Controller
public class LoginController {
@RequestMapping(value = "/test/{id}/{name}", method = RequestMethod.GET)
public String toLogin(@PathVariable String id, @PathVariable String name,
ModelMap model) {
System.out.println(id);
System.out.println(name);
model.addAttribute("message", "入门成功");
return "login";
}
}
6 工程目录截图
7 就涉及index.jsp, login.jsp, web.xml, springmvc-servlet.xml, LoginController.java
8 jar包下载地址:springmvc jar