Struts1.x —— 登录实验-无数据库版
前言
本实验在 IDEA 2021.2.2(UItimate Edition) 下运行。
使用以下软件、开发包:
JDK-14.0.1
struts-1.3.10
Apache Tomcat 9.0.53
注:请使用Apache Tomcat 9.x版本,亲测使用10.x版本会导致出错。原因是Struts1.x中的servlet-api版本与Apache Tomcat 10.x不兼容。
注:本文仅作为学习,不考虑任何生产环境,谨以此文记录所学,造福后人
环境配置
Apache Tomcat 9.0.53 配置
下载
官方地址:https://tomcat.apache.org/download-90.cgi
安装和配置
解压即可完成“安装”
如图所示:
打开conf文件夹中的 logging.properties 文件,
将java.util.logging.ConsoleHandler.encoding = UTF-8
修改为:java.util.logging.ConsoleHandler.encoding = GBK
运行bin目录下的 startup.bat
等待Tomcat服务器完成启动
浏览器访问:http://localhost:8080/
出现如下界面,表明启动成功:
然后运行bin目录下的 shutdown.bat关停Tomcat服务
注:请一定使用此方法关闭。即使关掉了之前的startup.bat窗口, 也请运行shutdown.bat关闭
IDEA中配置Tomcat
用IDEA打开任意工程,点击“运行”–>“编辑配置”
添加Tomcat服务器,选择本地服务器
然后按照如下步骤配置
配置部署工件:
创建新项目
选择Java EE8即可,其他的保持默认
创建成功后项目结构如下图:
直接运行进行测试
显示下图,即为成功
Struts 1.3.10配置
下载与安装
官网下载地址: https://archive.apache.org/dist/struts/1.3.10/struts-1.3.10-all.zip
解压即可。
实验步骤
创建项目
如上述测试Tomcat配置相同的方式创建项目
然后删除 HelloServlet.java
和 index.jsp
文件
导入struts
在WEB-INF目录下创建lib文件夹
将struts1.3.10中的lib文件夹下的所有文件拷贝
至本工程下的lib文件夹
即,如图:
然后,在IDEA中右击lib文件夹,选择“添加为库”
到此,struts库导入完成
配置web服务器
在WEB-INF文件夹下的web.xml
文件中添加
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
在WEB-INF文件夹下创建struts-config.xml
文件
注:web.xml配置的是Tomcat Web服务器对请求的响应(即,转发给struts管理),struts-config.xml配置的是struts如何管理请求
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
</struts-config>
struts初始配置完成
编写类
编写登录所用到的类
在main\java\com\example\strutsleaning 路径下
创建LoginActionForm.java
文件
package com.example.strutsleaning;
import org.apache.struts.action.ActionForm;
public class LoginActionForm extends ActionForm {
private String username;
private String password;
/**
* @return Returns the password.
*/
public String getPassword() {
return password;
}
/**
* @param password The password to set.
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return Returns the username.
*/
public String getUsername() {
return username;
}
/**
* @param username The username to set.
*/
public void setUsername(String username) {
this.username = username;
}
}
创建LoginAction.java
文件
package com.example.strutsleaning;
import org.apache.struts.action.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
//将ActionForm强制转换为LoginActionForm
LoginActionForm loginForm = (LoginActionForm) form;
//从LoginActionForm中提取从页面表单传递过来的参数
String username = loginForm.getUsername();
String password = loginForm.getPassword();
//根据这些参数,执行业务逻辑操作
if ("admin".equals(username) && "admin".equals(password)) {
//如果用户名和密码均为admin,则转向登录成功页面
return mapping.findForward("success");
} else {
//否则转向登录失败页面
return mapping.findForward("error");
}
}
}
编写jsp页面
在webapp文件夹下
创建login.jsp
登录主界面
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>请登录</title>
</head>
<body>
<form action="login.do" method="post">
请输入用户名:<input type="text" name="username"> <br/>
请输入密码:<input type="password" name="password"> <br/>
<input type="submit" name="submit1" value="登录">
</form>
</body>
</html>
创建login_success.jsp
登录成功界面
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>登录成功</title>
</head>
<body>
欢迎您,您已经成功登录!您创建的第一个Struts应用程序已成功运行!!!
</body>
</html>
创建login_error
登录失败界面
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>登录失败</title>
</head>
<body>
您的登录失败了,可能原因是用户名或密码不正确,请返回重新输入
<a href="login.jsp">返回登录页面</a>
</body>
</html>
配置struts-config.xml
在<struts-config></struts-config>
内添加如下内容
<form-beans>
<form-bean name="loginForm" type="com.example.strutsleaning.LoginActionForm"/>
</form-beans>
<action-mappings>
<action path="/login" type="com.example.strutsleaning.LoginAction" name="loginForm" scope="request">
<forward name="success" path="/login_success.jsp"/>
<forward name="error" path="/login_error.jsp"/>
</action>
</action-mappings>
运行测试
点击运行
等待浏览器弹出
输入http://localhost:8080/Struts_leaning_war_exploded/login.jsp
返回页面:
输入账号:admin
;密码:admin
成功登录!
若账号密码不正确
到此实验完成!
数据库版本,参见:Struts1.x 登录实验(数据库版)
感谢阅读!
有疑问或者认为有错误请留言,谢谢!