搭建一个简单struts2框架的登陆

时间:2023-03-09 00:47:30
搭建一个简单struts2框架的登陆

第一步:下载struts2对应的jar包,可以到struts官网下载:http://struts.apache.org/download.cgi#struts252

出于学习的目的,可以把整个完整的压缩文件都下载下来。

里面包括:1 apps:示例应用,对学习很有帮助 ; 2 docs:相关学习文档、API文档等; 3 lib:核心类库,依赖包; 4:src:源代码

第二步:在eclipse新建一个Dynamic Web Project类型工程,一直点next,记得勾选generate web.xml deployment descriptor,才能有web.xml

搭建一个简单struts2框架的登陆

第三步:导入jar包

struts2有个几个必备的jar包,可以去官网下载struts-min-lib.zip,里面的包就是必须要的jar包。

第四步:编写对应的Action

在src下面新建一个包com.struts.action(注意:命名需要时com.XXX.action的形式),然后再下面新建一个java文件:LoginAction.java

package com.struts.action;

import com.opensymphony.xwork2.ActionSupport;
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;
}
public String execute() throws Exception
{
if("username".equals(this.getUsername()) && "password".equals(this.getPassword()))
{
return "success";
}
else
{
return "fail";
} }
}

第五步:新建结果页面

在WEB-INF新建下面两个文件:

success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>success!</title>
</head>
<body>
success!
</body>
</html>

fail.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>fail!</title>
</head>
<body>
fail!
</body>
</html>

第六步:在WEB-INF下新建初始页面(根据LoginAction知道需要提交的表单里面有两个参数)

login.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login" method="post">
用户名:<input type="text" name="username">
密码:<input type="password" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>

第七步:配置在WEB-INF下的web.xml:1、加入struts过滤器 2、配置首页

<?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/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>struts2Project</display-name>
<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>
<welcome-file-list>
<welcome-file>/WEB-INF/login.html</welcome-file>
</welcome-file-list>
</web-app>

第八步:配置struts2配置文件,在src目录下新建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"> <!-- START SNIPPET: xworkSample -->
<struts>
<package name="com.struts.action" extends="struts-default">
<action name="login" class="com.struts.action.LoginAction">
<result name="success">/WEB-INF/success.jsp</result>
<result name="fail">/WEB-INF/fail.jsp</result>
</action>
</package>
</struts>

结尾:至此完成简单struts2框架的登陆搭建,放进tomcat并启动,就可以通过http://localhost:8080/struts2Project/ 访问。