struts2-6-登录注册校验(模型驱动)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了struts2-6-登录注册校验(模型驱动)相关的知识,希望对你有一定的参考价值。
一:用户封装到JavaBean中-->nuc.sw.vo-->User.java
1 package nuc.sw.vo; 2 3 public class User { 4 private String username; 5 private String password; 6 public String getUsername() { 7 return username; 8 } 9 public void setUsername(String username) { 10 this.username = username; 11 } 12 public String getPassword() { 13 return password; 14 } 15 public void setPassword(String password) { 16 this.password = password; 17 } 18 }
二:修改LoginRegAction.java 实例化模型User类,而且无set,get方法;
登录和注册方法要分开相对应
1 package nuc.sw.action; 2 3 import com.opensymphony.xwork2.ActionContext; 4 import com.opensymphony.xwork2.ActionSupport; 5 import com.opensymphony.xwork2.ModelDriven; 6 7 import nuc.sw.vo.User; 8 9 public class LoginRegAction extends ActionSupport implements ModelDriven<User>{
//实例化模型类 10 private User user=new User(); 11 12 //contextPath server.xml 13 //必须实现 getModel()方法 14 public User getModel() { 15 return user; 16 }; 17 //action 18 19 public String RegisterMethod() throws Exception { 20 // public String LoginMethod() throws Exception { 21 // TODO Auto-generated method stub 22 //注册逻辑 23 if(user.getUsername().equals("张丹")&&user.getPassword().equals("123")){ 24 ActionContext.getContext().getSession().put("registerUser",user.getUsername()); 25 ActionContext.getContext().getSession().put("registerPass",user.getPassword()); 26 return SUCCESS; 27 } 28 else{ 29 30 ActionContext.getContext().put("error", "用户名或者密码错误"); 31 return ERROR; 32 } 33 34 } 35 public String LoginMethod() throws Exception { 36 // TODO Auto-generated method stub 37 //登录逻辑 38 if(user.getUsername().equals("张丹")&&user.getPassword().equals("123")){ 39 ActionContext.getContext().getSession().put("loginUser",user.getUsername()); 40 ActionContext.getContext().getSession().put("loginPass",user.getPassword()); 41 return SUCCESS; 42 } 43 else{ 44 45 ActionContext.getContext().put("error", "用户名或者密码错误"); 46 return ERROR; 47 } 48 49 } 50 @Override 51 public void validate() { 52 // TODO Auto-generated method stub 53 if(user.getUsername()==null||user.getUsername().trim().equals("")) 54 this.addFieldError("usernameError", "<font color=‘red‘>用户名不能为空</font>"); 55 if(user.getPassword()==null||user.getPassword().trim().equals("")) 56 this.addFieldError("passwordError", "<font color=‘red‘>密码不能为空</font>"); 57 58 } 59 }
三:修改struts.xml -->action的method要分别对应
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="true" /> 8 <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> 9 <package name="default" namespace="/" extends="struts-default"> 10 <action name="LoginReg" method="LoginMethod" class="nuc.sw.action.LoginRegAction"> 11 <result name="success">/welcome.jsp</result> 12 <result name="error">/login.jsp</result> 13 <result name="input">/login.jsp</result> 14 </action> 15 <action name="RegisterReg" method="RegisterMethod" class="nuc.sw.action.LoginRegAction"> 16 <result name="success">/register.jsp</result> 17 <result name="error">/login.jsp</result> 18 <result name="input">/login.jsp</result> 19 </action> 20 </package> 21 <!-- Add packages here --> 22 </struts>
四:修改login.jsp-->form属性action-->添加按钮的事件-->实现相对应的onclick()方法
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 <script> 11 function login(){ 12 targetForm=document.forms[0]; 13 targetForm.action="LoginReg!LoginMethod"; 14 targetForm.submit(); 15 } 16 function register(){ 17 targetForm=document.forms[0]; 18 targetForm.action="RegisterReg!RegisterMethod"; 19 targetForm.submit(); 20 } 21 </script> 22 <body> 23 <s:fielderror escape="false"></s:fielderror> 24 <font color="red">${requestScope.error}</font> 25 <form action="actionName!methodName" method="post" > 26 用户名:<input type="text" name="username"><br> 27 密码:<input type="text" name="password"><br> 28 <input type="button" value="登录" onclick="login()"> 29 <input type="button" value="注册" onclick="register()"> 30 </form> 31 </body> 32 </html>
五:动态方法的调用
1 <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
六:register.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.registerUser}注册,请<a href="">查看详情</a> 11 </body> 12 </html>
七:项目结构
八:运行结果
(1)注册成功
(2)用户名和密码为空
(3)密码为空
(4)用户名为空
登录结果同上。
以上是关于struts2-6-登录注册校验(模型驱动)的主要内容,如果未能解决你的问题,请参考以下文章
41.登录注册小案例实现(使用Django中的form表单来进行用户输入数据的校验;文末附源码)