一、效果图
二、需要的Jar包
- 这里面包含了部分Spring的jar包,如果只用struts,只导入struts的即可
- 我是用idea构建的项目,当然也可以手动下载jar包,Jar包下载地址
三、项目结构及代码
login.jsp
<%--
Created by Leo.
Date: 10/16/2019
Time: 9:01 PM
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="login.action" method="post">
<label>
用户名
<input type="text" name="username">
</label><br>
<label>
密码
<input type="password" name="password">
</label><br>
<input type="submit" value="登录">
<input type="reset" value="重置">
</form>
</body>
</html>
index.jsp
<%--
User: Leo
Date: 10/16/2019
Time: 7:30 PM
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>主页</title>
</head>
<body>
<p1>欢迎---${param.username}---登录成功!</p1>
</body>
</html>
LoginAction
package action;
import com.opensymphony.xwork2.ActionSupport;
/** * @ClassName: LoginAction * @Author: Leo * @Description: * @Date: 10/16/2019 8:39 PM */
public class LoginAction extends ActionSupport {
private String username;
private String password;
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;
}
@Override
public String execute() throws Exception {
if ("admin".equals(username) && "123".equals(password)) {
return "success";
} else {
return "input";
}
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="leo" namespace="/" extends="struts-default">
<action name="login" class="action.LoginAction">
<result name="success">
index.jsp
</result>
<result name="input">
login.jsp
</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
<!--Struts2 核心控制器-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>