springMVC
1 package net.liuzd.web.action; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import net.liuzd.vo.User; 7 8 import org.springframework.stereotype.Controller; 9 import org.springframework.ui.Model; 10 import org.springframework.web.bind.annotation.ModelAttribute; 11 import org.springframework.web.bind.annotation.RequestMapping; 12 import org.springframework.web.servlet.ModelAndView; 13 14 @Controller 15 @RequestMapping("/user.do") 16 public class UserController { 17 18 @SuppressWarnings("unchecked") 19 @RequestMapping(params = "method=add") 20 public String login(@ModelAttribute("user") 21 User user, HttpServletRequest req, HttpServletResponse response) { 22 // 此处调用服务层进行相应的业务操作 23 System.out.println("用户信息: " + user); 24 // 传递对象于下一页面 25 req.setAttribute("user", user); 26 // 调用服务层进行验证用户,此处只演示功能 27 if ("spring".equals(user.getUserId()) 28 && "spring".equals(user.getUserPassword())) { 29 return "success"; 30 } 31 return "error"; 32 } 33 34 @SuppressWarnings("unchecked") 35 // @RequestMapping(params= "method=add2",method=RequestMethod.POST) 36 // 定义method方法不是必须的 37 @RequestMapping(params = "method=add2") 38 public ModelAndView login2(@ModelAttribute("user") 39 User user, HttpServletRequest req, HttpServletResponse response) { 40 41 // 此处调用服务层进行相应的业务操作 42 System.out.println("用户信息2: " + user); 43 // 使用ModelAndView保存对象于下一页面 44 ModelAndView model = new ModelAndView(); 45 model.addObject("user", user); 46 // 调用服务层进行验证用户,此处只演示功能 47 if ("spring".equals(user.getUserId()) 48 && "spring".equals(user.getUserPassword())) { 49 model.setViewName("success"); 50 } else { 51 model.setViewName("error"); 52 } 53 return model; 54 } 55 56 @RequestMapping(params = "method=add3") 57 public String login3(@ModelAttribute("user")User user,Model model) { 58 59 model.addAttribute("user",user); 60 System.out.println("用户信息3: "+user); 61 // 调用服务层进行验证用户,此处只演示功能 62 if ("spring".equals(user.getUserId()) && "spring".equals(user.getUserPassword())) { 63 return "success"; 64 } 65 //放入model默认把参数存于请求 66 model.addAttribute("msg","用户或者密码错误!"); 67 return "error"; 68 69 /** 70 * 重定向JSP页面,走出了springmvc配置的view(jsp) 71 * 因为这样说明:model.addAttribute("msg","用户或者密码错误!"); 72 * 获取不到值了 73 * 不加上.jsp就是这样:http://127.0.0.1:8080/Spring3-Login-Annotaction/index?msg=%E7... 74 * */ 75 //返回到页面是乱码在页面中用${msg}获取不到值,用request.getParams("msg");为乱码 76 //return "redirect:index.jsp?msg=用户或者密码错误!"; 77 } 78 79 80 }
1 package net.liuzd.vo; 2 3 public class User { 4 5 private String userId = null; 6 private String userPassword = null; 7 8 public String getUserId() { 9 return userId; 10 } 11 public void setUserId(String userId) { 12 this.userId = userId; 13 } 14 public String getUserPassword() { 15 return userPassword; 16 } 17 public void setUserPassword(String userPassword) { 18 this.userPassword = userPassword; 19 } 20 21 public User() { 22 23 } 24 25 public User(String userId2, String userPassword2) { 26 this.userId = userId2; 27 this.userPassword = userPassword2; 28 } 29 @Override 30 public String toString() { 31 return "userId: " + userId + ",userPassword: " + userPassword; 32 } 33 34 }
springmvc.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p="http://www.springframework.org/schema/p" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 11 <!-- 12 自动搜索@Controller标注的类 13 用于指明系统从哪个路径下寻找controller,然后提前初始化这些对象。 14 --> 15 <context:component-scan base-package="net.liuzd.web.action" /> 16 17 <!-- Default ViewResolver --> 18 <bean id="viewResolver" 19 20 21 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 22 <property name="viewClass" 23 value="org.springframework.web.servlet.view.JstlView" /> 24 <property name="prefix" value="/WEB-INF/jsp/" /> 25 <property name="suffix" value=".jsp"></property> 26 </bean> 27 28 </beans>
web.XML
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 5 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 6 7 <filter> 8 <filter-name>encodingFilter</filter-name> 9 <filter-class> 10 org.springframework.web.filter.CharacterEncodingFilter 11 </filter-class> 12 <init-param> 13 <param-name>encoding</param-name> 14 <param-value>UTF-8</param-value> 15 </init-param> 16 <init-param> 17 <param-name>forceEncoding</param-name> 18 <param-value>false</param-value> 19 </init-param> 20 </filter> 21 22 <filter-mapping> 23 <filter-name>encodingFilter</filter-name> 24 <url-pattern>*.do</url-pattern> 25 </filter-mapping> 26 27 <filter-mapping> 28 <filter-name>encodingFilter</filter-name> 29 <url-pattern>*.jsp</url-pattern> 30 </filter-mapping> 31 32 <servlet> 33 <servlet-name>springmvc</servlet-name> 34 <servlet-class> 35 org.springframework.web.servlet.DispatcherServlet 36 </servlet-class> 37 <init-param> 38 <param-name>contextConfigLocation</param-name> 39 <!-- 40 指定XML文件位置 41 <param-value>/WEB-INF/classes/springmvc.xml 42 <param-value>classpath*:springmvc.xml 43 --> 44 <!-- 在classpath路径下去寻找springmvc.xml文件 --> 45 <param-value>classpath:springmvc.xml 46 </param-value> 47 </init-param> 48 <load-on-startup>1</load-on-startup> 49 </servlet> 50 51 <servlet-mapping> 52 <servlet-name>springmvc</servlet-name> 53 <url-pattern>*.do</url-pattern> 54 </servlet-mapping> 55 <welcome-file-list> 56 <welcome-file>index.jsp</welcome-file> 57 </welcome-file-list> 58 59 </web-app>
index.JSP
1 <%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 <%@ include file="/common/meta.jsp"%> 6 </head> 7 <body> 8 <form action="${pageContext.request.contextPath}/user.do?method=add" method="post"> 9 ID号: <input type="text" name="userId" id="userId"><br> 10 密码: <input type="password" name="userPassword" id="userPassword"><br> 11 <input type="submit" value="Login"> 12 </form> <br> 13 <form action="${pageContext.request.contextPath}/user.do?method=add2" 14 15 method="post"> 16 ID号: <input type="text" name="userId" id="userId"><br> 17 密码: <input type="password" name="userPassword" id="userPassword"><br> 18 <input type="submit" value="Login2"> 19 </form> 20 <br> 21 <form action="${pageContext.request.contextPath}/user.do?method=add3" 22 23 method="post"> 24 ID号: <input type="text" name="userId" id="userId"><br> 25 密码: <input type="password" name="userPassword" id="userPassword"><br> 26 <input type="submit" value="Login3"> 27 </form> 28 </body> 29 </html>
error.jsp
1 <%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%> 2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 4 <html> 5 <head> 6 <%@ include file="/common/meta.jsp"%> 7 </head> 8 <body> 9 登录错误:ID号: ${user.userId},密码: ${user.userPassword},<font color='red'>${msg}</font><br> 10 <a href="${pageContext.request.contextPath}/index.jsp">返回</a> 11 </body> 12 </html>
success.jsp
1 <%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%> 2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 4 <html> 5 <head> 6 <%@ include file="/common/meta.jsp"%> 7 </head> 8 <body> 9 欢迎你: ${user.userId}<br> 10 <a href="${pageContext.request.contextPath}/index.jsp">返回</a> 11 </body> 12 </html>