该项目有一个实现登录和注册功能的页面。
下面是该项目的文件结构图:
1、编写视图界面(JSP页面)
loginRegister.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Action中的动态方法调用</title>
<script type="text/javascript">
function register(){
//获取页面第一个表单
targetForm=document.forms[0];
//动态修改表单的Action属性
targetForm.action="loginReg";
//表示提交函数
document.forms[0].submit();
}
</script>
</head>
<body>
<table width="360" align="center">
<form action="loginReg">
<tr>
<td>用户名:</td>
<td><input name="userName" type="text" size="26"/></td>
</tr>
<tr>
<td>密 码:</td>
<td><input name="passWord" type="password" size="26"/></td>
</tr>
<tr>
<td><input type="submit" value="登录"/></td>
<td><input type="submit" value="注册" onclick="register()"/></td>
</tr>
</form>
</table>
</body>
</html>
success2.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>成功页面</title>
</head>
<body>
<s:property value="msg"/>
</body>
</html>
2、编写业务控制器Action
LoginRegisterAction.java
package loginRegisterAction;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class loginRegisterAction extends ActionSupport{
private String userName;
private String passWord;
//设置返回信息
private String msg;
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;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
//Action包含的注册控制逻辑
public String regist() throws Exception{
ActionContext.getContext().getSession().put("userName", getUserName());
setMsg("恭喜你,"+userName+"注册成功!");
return SUCCESS;
}
//Action默认包含的控制逻辑
public String execute() throws Exception{
if(getUserName().equals("hjw")&&getPassWord().equals("123")) {
ActionContext.getContext().getSession().put("userName", getUserName());
setMsg("你单击的是登录!"+"你的登录名为"+userName+",登录成功!");
return SUCCESS;
}
else {
return INPUT;
}
}
}
3、编写web.xml和struts.xml
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaeee/web-app_3_1.xsd"
id="WebApp_ID"
version="3.1" >
<filter>
<!-- 配置Struts2核心控制器的名字 -->
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<!-- Struts2控制器的名字 -->
<filter-name>struts2</filter-name>
<!-- 拦截所有的URL请求-->
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>loginRegister.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="zzf" extends="struts-default">
<action name="loginReg" class="loginRegisterAction.loginRegisterAction" method="execute">
<result name="success">/loginRegister/success2.jsp</result>
</action>
<action name="loginReg" class="loginRegisterAction.loginRegisterAction" method="regist">
<result name="success">/loginRegister/success2.jsp</result>
</action>
</package>
</struts>
4、项目运行
登录成功界面:
注册成功界面: