首页 > 代码库 > struts2-4-登录校验(用户类型)
struts2-4-登录校验(用户类型)
一:登录页面:login.jsp
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <%@ taglib prefix="s" uri="/struts-tags"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 8 <title>登录页面</title> 9 </head>10 <body>11 <s:fielderror escape="false"></s:fielderror>12 <font color="red">${requestScope.error}</font>13 <form action="loginReg" method="post" >14 用户名:<input type="text" name="username"><br>15 密码:<input type="text" name="password"><br>
用户类型: <select name="userType"> <option value="http://www.mamicode.com/管理员">管理员</option> <option value="http://www.mamicode.com/普通用户">普通用户</option> </select>16 <input type="submit" value="http://www.mamicode.com/登录">17 </form>18 </body>19 </html>
二:跳转到action nuc.sw.action-->LoginRegAction
1 package nuc.sw.action; 2 3 import com.opensymphony.xwork2.ActionContext; 4 import com.opensymphony.xwork2.ActionSupport; 5 6 public class LoginRegAction extends ActionSupport{ 7 private String username; 8 private String password; 9 private String userType;10 public String getUsername() {11 return username;12 }13 14 public void setUsername(String username) {15 this.username = username;16 }17 18 public String getPassword() {19 return password;20 }21 22 public void setPassword(String password) {23 this.password = password;24 }25 public String getUserType() { return userType; } public void setUserType(String userType) { this.userType = userType; }26 @Override27 public String execute() throws Exception {28 // public String LoginMethod() throws Exception {29 // TODO Auto-generated method stub30 //登录逻辑31 if(username.equals("zd")&&password.equals("123")){32 ActionContext.getContext().getSession().put("user", username);33 ActionContext.getContext().getSession().put("pass", password);
ActionContext.getContext().getSession().put("type", userType);34 return SUCCESS;35 }36 else{37 38 ActionContext.getContext().put("error", "用户名或者密码错误");39 return ERROR;40 }41 42 }43 @Override44 public void validate() {45 // TODO Auto-generated method stub46 if(username==null||username.trim().equals(""))47 this.addFieldError("usernameError", "<font color=‘red‘>用户名不能为空</font>");48 if(password==null||password.trim().equals(""))49 this.addFieldError("passwordError", "<font color=‘red‘>密码不能为空</font>");50 51 }52 }
三:修改配置文件struts.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <constant name="struts.devMode" value="http://www.mamicode.com/true" /> 8 <package name="default" namespace="/" extends="struts-default"> 9 <action name="loginReg" class="nuc.sw.action.LoginRegAction">10 <result name="success">/welcome.jsp</result>11 <result name="error">/login.jsp</result>12 <result name="input">/login.jsp</result>13 </action>14 </package>15 <!-- Add packages here -->16 </struts>
四:登录成功页面 welcome.jsp
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>Insert title here</title> 8 </head> 9 <body>10 欢迎${sessionScope.type}${sessionScope.user}登录;11 </body>12 </html>
五:action标签的method属性的取值
1 public String LoginMethod() throws Exception 去@override
1 <action name="loginReg" class="nuc.sw.action.LoginRegAction" method="LoginMethod">
六:使用ActionSupport所提供的方法
1 @Override2 public void validate() {3 // TODO Auto-generated method stub4 if(username==null||username.trim().equals(""))5 this.addFieldError("usernameError", "用户名不能为空");6 if(password==null||password.trim().equals(""))7 this.addFieldError("passwordError", "密码不能为空");8 9 }
七:修改struts.xml 获取校验的数据
1 <result name="input">/login.jsp</result>
八:修改login.jsp 在登录页面显示错误信息
1 <%@ taglib prefix="s" uri="/struts-tags"%>2 <s:fielderror escape="false"></s:fielderror>
九:项目结构:
十:运行结果:
正确:
?
错误:
数据校验:
struts2-4-登录校验(用户类型)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。