【例】使用通配符实现用户登录与注册
第一步:编写action
package com.dwx.actions;
import com.opensymphony.xwork2.ActionSupport;
public class userAction extends ActionSupport{private String username;private String password;private String age;private String sex;public String getAge() {return age;}public void setAge(String age) {this.age = age;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String login(){return SUCCESS;}public String regist(){return "regist_info";}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}
}
第二步:配置struts2.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd">
<struts><package name="user" extends="struts-default" namespace="/"><action name="user*" class="com.dwx.actions.userAction" method="{1}"><result name="success">/success.jsp</result><result name="regist_info">/regist_info.jsp</result></action></package>
</struts>
第三步:编写登录页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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>用户登录</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">--><style type="text/css">a{text-decoration: none;}</style></head> <body><center><s:form action="userlogin" method="post" name="loginForm"><s:textfield name="username" label="用户名"></s:textfield><s:password name="password" label="密码"></s:password><s:submit value="登录"></s:submit><s:a href="regist.jsp">注册</s:a> </s:form></center></body>
</html>
第四步:编写注册页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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>注册页面</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><center><s:form action="userregist" method="post"><s:textfield name="username" label="用户名"></s:textfield><s:password name="password" label="密码"></s:password><s:textfield name="age" label="年龄"></s:textfield><s:radio name="sex" label="性别" list="{'男','女' }" value="男"></s:radio><s:submit value="注册"></s:submit></s:form></center></body>
</html>
第五步:编写登录成功页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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>登录成功</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><h3 align="center">登录成功</h3><hr><center>欢迎:<s:property value="username" /></center></body>
</html>
第六步:编写注册成功页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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>注册信息</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><h3 align="center">注册信息</h3><hr><center>用户名:<s:property value="username" /><br>密码:<s:property value="password" /><br>年龄:<s:property value="age"/><br>性别:<s:property value="sex"/><br></center> </body>
</html>
效果:
登录页面(login.jsp)

注册页面(regist.jsp)

登录成功页面(success.jsp)

注册成功页面(regist_info.jsp)
