实验一 基于Struts2的登录系统实现
一、目的与任务
目的:熟悉在集成开发平台上配置Struts2
框架,学习Struts2
编程
任务:在集成开发平台上创建Struts2
项目,实现登录系统
二、内容、要求与安排方式
1、实验内容与要求:
-
下载
Struts2
软件包并了解Struts2
软件包中主要文件; -
学习在集成开发平台上配置
Struts2
框架,熟悉Struts2
框架编程; -
实现基于
Struts2
的登录系统,若正确页面跳转到成功页面,不正确页面跳转到登录页面。
可选题:修改登录系统连接数据库,访问数据库判断输入的用户名和密码是否存在。
2、 具体操作步骤:
-
在
Intellij IDEA
平台上创建Dynamic Web Project
。Dynamic Web Project
是Eclipse
下的表述,在IDEA
中应当选择Web Application
,然后选择Struct2
,如图所示: -
添加
Struts2
框架常用jar包至项目lib文件夹,Struts2
框架常用jar包有:commons-fileupload-1.2.2.jar
、commons-io-2.0.1.jar
、freemarker-2.3.16.jar
、javassist-3.11.0.GA.jar
、ognl-3.0.1.jar
、struts2-core-2.2.3.1.jar
、xwork-core-2.2.3.1.jar
、commons-lang3-3.1.ja
r、commons-logging-1.1.1.jar
在下放选择
Use Library
->create
,导入已经下载完成的Struts2
框架常用jar包。使用的Struct2
版本是 2.5.22 2.5.22 2.5.22配置struct2 项目最终的整体结构如下
-
在
web.xml
中配置核心控制器FilterDispatcher
<?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"> <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>
web.xml -
编写视图组件
login.jsp
,success.jsp
,fail.jsp
<%-- Created by IntelliJ IDEA. User: Lunatic Date: 2020/5/6 Time: 9:03 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>基于Struts2的登录系统应用</title> </head> <body> <form method="post" action="login"> 用户名:<input name="userName" type="text" size="26"> <br> 密码:<input name="passWord" type="password" size="26"> <br> <input type="submit" value="登录"> </form> <hr> </body> </html>
登录界面login.jsp
<%--
Created by IntelliJ IDEA.
User: Lunatic
Date: 2020/5/6
Time: 9:08
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录成功页面</title>
</head>
<body>
<h1>你登录成功,欢迎你!!!</h1>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: Lunatic
Date: 2020/5/6
Time: 15:20
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>抱歉,登陆失败</title>
</head>
<body>
<h1>请检查登录名或密码是否正确</h1>
</body>
</html>
- 编写登录页面对应的业务逻辑组件
LoginBean
类
package LoginBean;
public class LoginBean {
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;
}
public boolean login(String userName, String passWord) {
boolean b = false;
if(userName.equals("Lunatic") && passWord.equals("000000")) {
b = true;
}
else {
b = false;
}
return b;
}
}
- 编写业务控制器
LoginAction.java
package LoginAction;
import LoginBean.LoginBean;
public class LoginAction {
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;
}
public String execute() throws Exception{
LoginBean lb = new LoginBean();
if(lb.login(userName, passWord)) {
return "success";
}
else {
return "error";
}
}
}
-
struts.xml
中配置Action<?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="zzf" extends="struts-default" namespace="/"> <action name="login" class="LoginAction.LoginAction"> <result name="error">/fail.jsp</result> <result name="success">/login/success.jsp</result> </action> </package> </struts>
struct.xml -
启动服务器,运行页面即可。本地页面地址http://localhost:8080/First_Web_Project_war_exploded/_
本地Tomcat服务器启动 登录界面打开正常