2019独角兽企业重金招聘Python工程师标准>>>
1. 搭建环境
2. 如何完成Controller和Viewer的映射
3. 如何把值传递给Controller
4. Controller如何把值传递给Viewer
5. 异常处理
6. 页面标签
7. 文件上传
8. 深入一下源代码
Spring MVC流程图:
1. 环境搭建
web.xml
spring-servlet.xml
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
{@Overrideprotected 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
<%&#64; page language&#61; "java" contentType &#61; "text/html; charset&#61;utf-8"pageEncoding &#61;"utf-8" %>
< html>
< head>
< meta http-equiv &#61;"Content-Type" content&#61; "text/html; charset&#61;utf-8">
< title> Insert title here
head>
< body> Hello&#xff01;${userName } h1 >${string }
body>
html>
spring-servlet.xml
xml version &#61;"1.0" encoding&#61; "UTF-8" ?>
< beans xmlns &#61;"http://www.springframework.org/schema/beans"xmlns:xsi &#61;"http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc &#61;"http://www.springframework.org/schema/mvc"xmlns:context &#61;"http://www.springframework.org/schema/context"xsi:schemaLocation &#61;"http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd">
beans>
源码&#xff1a; http://pan.baidu.com/s/1o6A0YpG