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>
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>springmvc2</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc2</servlet-name>
<url-pattern>*.action</url-pattern> <!--配置的访问路径,一定是按照这种格式写 -->
</servlet-mapping>
</web-app>
3.配置SpringMVC
3.1 方法一 手动配置
springmvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
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">
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter">
</bean>
<bean id="/hello.action" class="com.smvc.action.HController"> </bean>
<pre name="code" class="html"> <!-- <span style="font-family: Arial, Helvetica, sans-serif;"> 配置处理映射器 </span><span style="font-family: Arial, Helvetica, sans-serif;"> --></span>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- 手动添加action的bean
-->
<bean class="com.smvc.action.HController"></bean> <!-- 引入解析jstl的类 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean></beans>
HController.java
package com.smvc.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class HController implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest requset,
HttpServletResponse response) throws Exception {
ModelAndView m=new ModelAndView(); //向页面传数据..
m.addObject("hello","haha");
m.setViewName("/index.jsp");
return m;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>springMVC</title>
</head>
<body>
${hello}<br>
</body>
</html>
3.2 方法二 用扫描和注释
web.xml 不变
springmvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
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">
<context:annotation-config>
<mvc:annotation-driven>
</mvc:annotation-driven>
</context:annotation-config>
<!-- 用扫描的方式把包加入到bean 中 -->
<context:component-scan base-package="com.smvc.action"></context:component-scan>
<!-- 引入解析jstl的类 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
</beans>
testAction.java
package com.smvc.action;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class testAction {
/*
* 注释方法
*/
@RequestMapping(value="/test.action") //地址映射的注释
public ModelAndView gettest(){
ModelAndView m=new ModelAndView(); //向页面传值用。。。
m.addObject("hello", "leaf");
m.setViewName("/index.jsp");
return m;
}
}
jsp页面同上