1、使用spring MVC 需要导入相关jar包
2、web.xml 启用spring MVC
<servlet>
<servlet-name>spring3mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定配置文件所在目录src 默认目录在/WEB-INF/ 默认文件名<servlet-name>-servlet.xml -->
<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>spring3mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3、编写Controller 使用xml配置文件方式需要继承Controller
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 HelloWorld implements Controller { @Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
String value = "nihao";
//返回视图 参数1为显示页面因为后面配置文件配置了后缀所以这里指需要给出页面名称不需要后缀
// 传参使用键值对 参数会绑定到request域
return new ModelAndView("/test", "Key", value);
} }
4、显示页面test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>
使用EL表达式取值
Ok!!!!!!!!!!! ${key }
</body>
5、spring MVC配置文件 单独Controlle配置
<?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" xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<bean name="/test" class="com.ly.Controller.HelloWorld"></bean> <!-- 定义跳转的文件的前后缀 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀就是指定你的显示页面根目录 -->
<property name="prefix" value="/" />
<!-- 后缀给出显示页面后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
6、一个controller编写多个方法 需要继承 MultiActionController (同样使用xml配置文件方式)
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController; public class multiController extends MultiActionController { public ModelAndView add(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("add");
return new ModelAndView("/test", "key", "value");
} public ModelAndView update(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("update");
return new ModelAndView("/test", "key", "value");
}
}
7、一个controller 编写多个方法 配置文件配置
<?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" xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<bean name="/testc" class="com.ly.Controller.multiController">
<property name="methodNameResolver" ref="methodNameResolver"></property>
</bean> <!-- 一个controller 编写多个方法 解析 -->
<bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="action"></property>
</bean> <!-- 添加注解驱动 -->
<mvc:annotation-driven />
<!-- 定义跳转的文件的前后缀 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀就是指定你的显示页面根目录 -->
<property name="prefix" value="/" />
<!-- 后缀给出显示页面后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>