昨天说的那个hibernate太简单了,所以就没发出来,今天教spring+springmvc
//LoginAction.java
package org.mo.spring; import java.util.HashMap; import java.util.Map; import org.springframework.validation.BindException; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.SimpleFormController; public class LoginAction extends SimpleFormController { private String fail_view; private String success_view; private int login(LoginInfo info) { if (!"".equals(info.getAccount()) && !"".equals(info.getPassword())) { if (UserInfo.exitsUser(info.getAccount())) { if (UserInfo.confirmPassword(info.getAccount(), info.getPassword())) { return 1;//登录成功 } else { return 2;//用户密码不正确 } } else { return 3;//用户不存在 } } else { return 0;//填写信息有误 } } @Override protected ModelAndView onSubmit(Object command, BindException errors) throws Exception { LoginInfo loginInfo = (LoginInfo) command; String message = null; Map<String, String> map = new HashMap<String, String>(); if (login(loginInfo) == 0) { message = "用户名或密码为空"; map.put("msg", message); return new ModelAndView(this.fail_view, map); } else if (login(loginInfo) == 3) { message = "用户不存在"; map.put("msg", message); return new ModelAndView(this.fail_view, map); } else if (login(loginInfo) == 2) { message = "用户密码不正确"; map.put("msg", message); return new ModelAndView(this.fail_view, map); } else { message = "登录成功"; map.put("msg", message); return new ModelAndView(this.success_view, map); } } public String getFail_view() { return fail_view; } public void setFail_view(String fail_view) { this.fail_view = fail_view; } public String getSuccess_view() { return success_view; } public void setSuccess_view(String success_view) { this.success_view = success_view; } }
//LoginInfo.java
package org.mo.spring; public class LoginInfo implements java.io.Serializable { private String account; private String password; public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
//UserInfo.java
package org.mo.spring; import java.util.HashMap; import java.util.Map; public class UserInfo { private static Map<String, String> userInfo = new HashMap<String, String>(); static { String user_one = "admin"; String password_one = "123456"; String user_two = "neusoft"; String password_two = "neusoft"; userInfo.put(user_one, password_one); userInfo.put(user_two, password_two); } public static boolean exitsUser(String username) { return userInfo.containsKey(username); } public static boolean confirmPassword(String username, String password) { return userInfo.get(username).endsWith(password); } }
//applicationContext.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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <bean id="loginAction" class="org.mo.spring.LoginAction"> <property name="commandClass"> <value>org.mo.spring.UserInfo</value> </property> <property name="fail_view"> <value>/error.jsp</value> </property> <property name="success_view"> <value>/main.jsp</value> </property> </bean> <bean id="f" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/login.do">loginAction</prop> </props> </property> </bean> </beans>
//login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘login.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="login.do" method="post"> 用户名:<input type="text" name="account"><br/> 密 码:<input type="password" name="password"><br/> <input type="submit" value="提交"> </form> </body> </html>
//error.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘error.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 很遗憾,登录失败,失败原因是<%=request.getAttribute("msg") %> </body> </html>
时间: 2024-10-28 21:40:36