一:登录主页面--->login
根据登录注册的不同按钮进入不同的action及method
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 <script> 10 function login(){ 11 targetForm=document.forms[0]; 12 targetForm.action="Login!LoginMethod"; 13 targetForm.submit(); 14 } 15 function register(){ 16 targetForm=document.forms[0]; 17 targetForm.action="Register!RegisterMethod"; 18 targetForm.submit(); 19 } 20 </script> 21 </head> 22 23 <body> 24 <s:fielderror escape="false"></s:fielderror> 25 <font color="red">${requestScope.error}</font> 26 <form action="actionName!methodName" method="post" > 27 用户名:<input type="text" name="username"><br> 28 密 码:<input type="text" name="password"><br> 29 详细地址:<input type="text" name="address">省 30 <input type="text" name="address">市 31 <input type="text" name="address">区<br> 32 33 34 <input type="text" name="address"/><br> 35 用户类型: 36 <select name="usertype"> 37 <option value="管理员" >管理员</option> 38 <option value="普通用户">普通用户</option> 39 </select> 40 <br/> 41 <input type="button" value="登录" onclick="login()"> 42 <input type="button" value="注册" onclick="register()"> 43 </form> 44 </body> 45 </html>
二:注册页面--->register.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=UTF-8"> 8 <title>注册页面</title> 9 </head> 10 <body> 11 <form action="Register" method="post"> 12 用户名:<input type="text" name="username" /><br> 13 密码:<input type="password" name="password" /><br> 14 详细地址:<input type="text" name="address">省 15 <input type="text" name="address">市 16 <input type="text" name="address">区<br> 17 <input type="text" name="address"/> 18 <input type="submit" value="注册"> 19 </form> 20 </body> 21 </html>
三:用户登录成功页面 --->adminWelcome.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.loginType}${sessionScope.loginUser}<a href="login.jsp">登录!</a><br> 11 12 </body> 13 </html>
四:用户个人的相关信息 nuc.sw.vo.User.java
1 package nuc.sw.vo; 2 3 public class User { 4 private String username; 5 private String password; 6 private String usertype; 7 private Address address; 8 public Address getAddress() { 9 return address; 10 } 11 public void setAddress(Address address) { 12 this.address = address; 13 } 14 public String getUsername() { 15 return username; 16 } 17 public void setUsername(String username) { 18 this.username = username; 19 } 20 public String getPassword() { 21 return password; 22 } 23 public void setPassword(String password) { 24 this.password = password; 25 } 26 public String getUsertype() { 27 return usertype; 28 } 29 public void setUsertype(String usertype) { 30 this.usertype = usertype; 31 } 32 33 }
五:地址是四级联为了简化应使用一个变量来获取四个值 nuc.sw.vo--->Address.java
1 package nuc.sw.vo; 2 3 public class Address { 4 5 private String province; 6 private String city; 7 private String county; 8 private String street; 9 10 public String getProvince() { 11 return province; 12 } 13 14 public void setProvince(String province) { 15 this.province = province; 16 } 17 18 public String getCity() { 19 return city; 20 } 21 22 public void setCity(String city) { 23 this.city = city; 24 } 25 26 public String getCounty() { 27 return county; 28 } 29 30 public void setCounty(String county) { 31 this.county = county; 32 } 33 34 public String getStreet() { 35 return street; 36 } 37 38 public void setStreet(String street) { 39 this.street = street; 40 } 41 42 }
六:现将地址转换,需加一个 文件:xwork-conversion.properties
1 nuc.sw.vo.Address=nuc.sw.utils.MyTypeConverter
七:在工具类中设置地址的值 --->nuc.sw.utils MyTypeConverter
1 package nuc.sw.utils; 2 3 import java.util.Map; 4 5 import org.apache.struts2.util.StrutsTypeConverter; 6 7 import nuc.sw.vo.Address; 8 9 public class MyTypeConverter extends StrutsTypeConverter { 10 11 @Override 12 public Object convertFromString(Map context, String[] values, Class toClass) { 13 14 Address address = new Address(); 15 address.setProvince(values[0]); 16 address.setCity(values[1]); 17 address.setCounty(values[2]); 18 address.setStreet(values[3]); 19 return address; 20 } 21 22 @Override 23 public String convertToString(Map context, Object o) { 24 25 return null; 26 } 27 28 }
八:nuc.sw.action--->LoginRegAction.java
在相关的Action中执行相应方法并且配置相应的存储区域以便获取
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 public User getModel() { 12 return user; 13 }; 14 public String LoginMethod() throws Exception { 15 //登录逻辑 16 if(user.getUsername().equals("张丹")&&user.getPassword().equals("123")){ 17 ActionContext.getContext().getSession().put("loginUser",user.getUsername()); 18 ActionContext.getContext().getSession().put("loginType",user.getUsertype()); 19 return SUCCESS; 20 } 21 else{ 22 ActionContext.getContext().put("error", "用户名或者密码错误"); 23 return ERROR; 24 } 25 } 26 public String RegisterMethod() throws Exception { 27 ActionContext.getContext().getSession().put("registerUser", user.getUsername()); 28 ActionContext.getContext().getSession().put("registerProvince",user.getAddress().getProvince()); 29 ActionContext.getContext().getSession().put("registerCity",user.getAddress().getCity()); 30 ActionContext.getContext().getSession().put("registerCounty",user.getAddress().getCounty()); 31 ActionContext.getContext().getSession().put("registerStreet",user.getAddress().getStreet()); 32 return SUCCESS; 33 } 34 35 }
九:配置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="false" /> 8 <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> 9 <package name="default" namespace="/" extends="struts-default"> 10 <action name="Login" method="LoginMethod" class="nuc.sw.action.LoginRegAction"> 11 <result name="success">/adminWelcome.jsp</result> 12 <result name="error">/login.jsp</result> 13 <result name="input">/login.jsp</result> 14 </action> 15 <action name="Register" method="RegisterMethod" class="nuc.sw.action.LoginRegAction"> 16 <result name="success">/regLogin.jsp</result> 17 <result name="error">/register.jsp</result> 18 <result name="input">/register.jsp</result> 19 </action> 20 </package> 21 </struts>
十:注册成功后,显示注册地址信息--->regLogin.jsp
可通过EL表达式/OGNL表达式获取相关值
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 欢迎${sessionScope.registerUser}注册,<a href="login.jsp">请登录!</a><br> 12 您的详细地址为:<br> 13 ${sessionScope.registerProvince} 14 ${sessionScope.registerCity} 15 <s:property value="#session.registerCounty"/> 16 <s:property value="#session.registerStreet"/> 17 </body> 18 </html>
十一:项目结构:
十二:运行结果:
时间: 2024-10-09 22:06:31