1. 搭建环境
2. 如何完成Controller和Viewer的映射
3. 如何把值传递给Controller
4. Controller如何把值传递给Viewer
5. 异常处理
6. 页面标签
7. 文件上传
8. 深入一下源代码
Spring MVC流程图:
1. 环境搭建
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> <servlet > <servlet-name> spring</servlet-name > <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class > <load-on-startup> 1</ load-on-startup> </servlet > <servlet-mapping > <servlet-name> spring</servlet-name > <url-pattern> /</ url-pattern> </servlet-mapping > </web-app>
spring-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: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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="controller" /> <mvc:annotation-driven /> <bean name="/welcome.html" class="controller.WelcomeController" ></bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean > </beans>
WelcomeController
package controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; public class WelcomeController extends AbstractController { @Override protected ModelAndView handleRequestInternal(HttpServletRequest request , HttpServletResponse response) throws Exception { System. out.println("welcome" ); return new ModelAndView("welcome"); } }
HelloController
package controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController { //RequestMapping表示用哪個url 來對應 @RequestMapping({ "/hello","/" }) public String hello() { System. out.println("hello" ); return "hello" ; } @RequestMapping( "/welcome") public String welcome() { return "welcome" ; } }
源码:http://pan.baidu.com/s/1eQtgvfW
2. 如何完成Controller和Viewer的映射
注意:
package controller; import org.springframework.stereotype.Controller ; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class HelloController { //RequestMapping表示用哪個url 來對應 // @RequestParam ("userName" )如果你不传userName值过来的话会报错,可以省略,不传值为null @RequestMapping({ "/hello","/" }) public String hello( @RequestParam("userName" ) String userName) { System. out.println("hello" ); System. out.println(userName ); return "hello" ; } @RequestMapping( "/welcome") public String welcome() { return "welcome" ; } }
HelloController
package controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController { //RequestMapping表示用哪個url 來對應 @RequestMapping({ "/hello" ,"/" }) public String hello(String userName ,Model model ) { System. out .println("hello" ); model .addAttribute("userName" , userName ); //此时用哪个作变key?它是默认使用的对象的类型作为 key -->model.addAttribute("string", userName); //model.addAttribute(new user()); -->model.addAttribute("user", new user()); model .addAttribute(userName ); System. out .println(userName ); return "hello" ; } @RequestMapping( "/welcome" ) public String welcome() { return "welcome" ; } }
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> <h1 > Hello!${userName }</ h1 > ${string } </ body> </ html>
spring-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: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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package ="controller" /> <mvc:annotation-driven /> <bean id= "viewResolver" class ="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value ="/WEB-INF/jsp/" /> <property name= "suffix" value =".jsp" /> </bean > </ beans>
源码: http://pan.baidu.com/s/1o6A0YpG