简单的Maven+SpringMVC
一、SpringMVC非注解编程
1:修改pom.xml文件(相当于非Maven项目的导入jar包)
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.5.RELEASE</version> </dependency>
2:修改配置文件web.xml为以下代码
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3:在src/main/resources下面创建springmvc-config.xml文件
<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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" /> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> <bean name="/hello" class="com.gx.controller.HelloController"/> </beans>
4:在src/main/java里面创建com.gx.controller.HelloController
package com.gx.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 HelloController implements Controller{ @Override public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response)throws Exception{ System.out.println("handleRequest is working ......"); String myGetMess = request.getParameter("mess"); System.out.println("what I get is "+myGetMess); ModelAndView mv = new ModelAndView(); mv.addObject("message" , "hello , this is my springmvc"); mv.setViewName("/index.jsp"); return mv; } }
5:在webapp下面创建跳转页面index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> ${requestScope.message} </body> </html>
6:在页面输入网址
http://localhost:8888/testSystem/hello?mess="123"
7:页面信息
8:控制台信息
二、SpringMVC注解编程
1:修改pom.xml文件(相当于非Maven项目的导入jar包)
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.5.RELEASE</version> </dependency>
2:修改配置文件web.xml为以下代码
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3:在src/main/resources下面创建springmvc-config.xml文件
<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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 非注解编程的配置文件 <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" /> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> <bean name="/hello" class="com.gx.controller.HelloController"/> --> <!-- spring会自动扫描 base-package 下面的包或者java文件 --> <context:component-scan base-package="com.gx.controller" /> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" /> </beans>
4:在src/main/java里面创建com.gx.controller.HelloController
package com.gx.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import org.springframework.stereotype.Controller; //public class HelloController implements Controller{ @Controller public class HelloController{ //public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response)throws Exception{ @RequestMapping(value="/hello") public ModelAndView useNote(){ System.out.println("method useNote in controller HelloController is working ......"); ModelAndView mv = new ModelAndView(); mv.addObject("message" , "hello , this is my springmvc"); mv.setViewName("/index.jsp"); return mv; } }
5:在webapp下面创建跳转页面index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> ${requestScope.message} </body> </html>
6:在页面输入网址
http://localhost:8888/testSystem/hello
7:页面信息
8:控制台信息
三、注册编程获取页面值并跳转
(3.1)ModelAndView
1:在“二”的基础上添加com.gx.model.User
package com.gx.model; public class User { String username; String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [username=" + username + ", password=" + password + "]"; } }
2:创建com.gx.controller.Controller1
package com.gx.controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.gx.model.User; import org.springframework.stereotype.Controller; @Controller public class Controller1{ @ModelAttribute public void useModelAndView(String name,String password,ModelAndView mv){ System.out.println("useModelAndView in Controller1 is working......"); User user = new User(); user.setPassword(password); user.setUsername(name); mv.addObject("user",user); } @RequestMapping(value="/login1") public ModelAndView login(ModelAndView mv){ System.out.println("login in Controller1 is working......"); User user = (User)mv.getModel().get("user"); System.out.println(user); //跳转到welcome.jsp mv.setViewName("/welcomePage.jsp"); return mv; } }
3:创建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> <form action="login1" method="post"> 请输入用户名 <input type="text" name="name" /> 请输入密码 <input type="password" name="password" /> <input type="submit" value="登录" /> </form> </body> </html>
4:创建welcomPage.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> ${requestScope.user.username},欢迎!! </body> </html>
5:在页面输入网址
6:点击登录之后的页面信息
7:控制台信息
(3.2)ModelMap
1:在(3.1)的基础上创建com.gx.controller.Controller2
package com.gx.controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import com.gx.model.User; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; @Controller public class Controller2{ @ModelAttribute public void useModelMap(String name,String password,ModelMap modelMap){ System.out.println("useModelMap in Controller2 is working......"); User user = new User(); user.setPassword(password); user.setUsername(name); modelMap.addAttribute("user",user); } @RequestMapping(value="/login2") public String login(ModelMap modelMap){ System.out.println("login in Controller2 is working......"); /* * 在Controller1里面 * User user = (User)mv.getModel().get("user"); */ User user = (User)modelMap.get("user"); System.out.println(user); /* * 在Controller1里面跳转到welcome.jsp * mv.setViewName("/welcomePage.jsp"); * return mv; */ //跳转到welcome.jsp return "welcomePage.jsp"; } }
2:修改index.jsp里面form表单action为login2
3:在页面输入网址
4:点击确定之后页面信息
5:控制台信息
(3.3)Model
1:在(3.1)的基础上创建com.gx.controller.Controller3
package com.gx.controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import com.gx.model.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @Controller public class Controller3{ @ModelAttribute public void useModel(String name,String password,Model model){ System.out.println("useModel in Controller3 is working......"); User user = new User(); user.setPassword(password); user.setUsername(name); model.addAttribute("user",user); } @RequestMapping(value="/login3") public String login(Model model){ System.out.println("login in Controller3 is working......"); /* * 在UseModelController1里面 * User user = (User)mv.getModel().get("user"); * 在UseModelController2里面 * User user = (User)modelMap.get("user"); */ User user = (User)model.asMap().get("user"); System.out.println(user); //跳转到welcome.jsp return "welcomePage.jsp"; } }
2:修改index.jsp里面form表单action为login3
3:在页面输入网址
4:网页信息
5:控制台信息
(3.4)@RequestParam
1:在(3.1)的基础上创建com.gx.controller.Controller4
package com.gx.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import com.gx.model.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @Controller public class Controller4{ @RequestMapping(value="/login4") public String login(@RequestParam("name")String username, @RequestParam("password")String password, Model model){ System.out.println("login in Controller4 is working......"); User user = new User(); user.setPassword(password); user.setUsername(username); System.out.println(user); model.addAttribute("user",user); //跳转到welcome.jsp return "welcomePage.jsp"; } }
2:修改index.jsp里面form表单action为login4
3:在页面输入网址
4:页面信息
5:控制台信息