目录结构如下
首先导入Struts 2 core Libraries包,右击项目选址myeclipse选项的add Struts2即可
1.代码:Login下的LoginAction.java
package Login;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String userName;
private String password;
private String mess = ERROR;
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 void validate(){
if(this.getUserName()==null || this.getUserName().length()==0){
addFieldError("userName", "用户名不能为空");
}
if(this.getPassword()==null || this.getPassword().length()==0){
addFieldError("password", "密码不能为空");
}else{
mess=SUCCESS;
}
}
public String execute(){
return mess;
}
}
2.struts.xml代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="login" extends="struts-default">
<action name="login" class="Login.LoginAction">
<result name="success">/success.jsp</result>
<result name="error">/login.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
3.web.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping></web-app>
4.login.jsp代码:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>基于Struts2的登录系统</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="ExtJs/resources/css/ext-all.css" />
<script type="text/javascript" src="ExtJs/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="ExtJs/ext-all.js"></script>
-->
</head>
<body>
<center>
<s:form action="login" method="post">
<table border="1" align="center" bgcolor="#AABBCCDD">
<tr><td><s:textfield name="userName" label="用户名字" size="18"/></td></tr>
<tr><td><s:password name="password" label="用户密码" size="18"/></td></tr>
<tr><td><s:submit value="登录"/></td></tr>
</table>
</s:form>
</center>
</body>
</html>
5.登录成功界面:success.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<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="ExtJs/resources/css/ext-all.css" />
<script type="text/javascript" src="ExtJs/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="ExtJs/ext-all.js"></script>
-->
</head>
<body>
${userName }登录成功!
</body>
</html>
运行流程如下:
首先获得login.jsp的请求,在web.xml中被拦截,到struts.xml中找到需要执行的action:login,执行类LoginAction.java,返回mess信息,struts.xml中的result会根据返回的信息执行下一步操作,如果成功则跳转到success.jsp,失败则返回login.jsp页面中
本文详细介绍了如何使用Struts2框架实现基于Web的登录系统,包括配置目录结构、编写Action类、配置struts.xml文件、实现login.jsp页面以及登录流程运行逻辑。
785

被折叠的 条评论
为什么被折叠?



