步骤如下:
一、下载struts包
百度直接搜“struts2”,进入Apache官网下载,此处下载的是struts-2.3.20-all.zip。
二、新建web项目
打开Eclipse新建一个Dynamic Web Project,项目名为:struts2。
三、向Web项目中添加struts2框架
1、解压struts-2.3.20-all.zip,在lib文件加下找到以下jar文件:
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
commons-lang3-3.2.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-core-2.3.20.jar
xwork-core-2.3.20.jar
2、把以上文件,复制粘贴到第二步新建的Web项目下的WebContent/WEB-INF/lib路径下。
3、修改WebContent/WEB-INF路径下的web文件:
<?xml version="1.0"encoding="UTF-8"?>
<web-app xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>struts 2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 以下是添加struts2框架 -->
<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>
四、建立struts2项目文件
1、在项目中的JavaResources/src路径下新建RegisterCheck.java跟RegisterAction.java文件(属于com.struts2包):
RegisterCheck.java代码:
package com.struts2;
publicclassRegisterCheck
{
publicboolean check(String psd,String repsd)
{
if (psd.equals(repsd))
{
returntrue;
}
else
{
returnfalse;
}
}
}
RegisterAction.java代码:
package com.struts2;
publicclassRegisterAction
{
private String usernaem;
private String psd;
private String repsd;
public String getUsernaem()
{
returnusernaem;
}
publicvoid setUsernaem(Stringusernaem)
{
this.usernaem = usernaem;
}
public String getPsd()
{
returnpsd;
}
publicvoid setPsd(String psd)
{
this.psd = psd;
}
public String getRepsd()
{
returnrepsd;
}
publicvoid setRepsd(String repsd)
{
this.repsd = repsd;
}
public String execute() throws Exception
{
RegisterCheckrc=newRegisterCheck();
if (rc.check(getPsd(),getRepsd()))
{
return"success";
}
else
{
return"failure";
}
}
2、在项目中的JavaResources/src路径下新建一个struts.xml文件,并在此文件中对项目的Action进行配置:
struts.xml文件内容:
<?xml version="1.0"encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD StrutsConfiguration 2.3//EN"
"
http://struts.apache.org/dtds/struts-2.3.dtd"><struts>
<package name="struts2" extends="struts-default">
<action name="register" class="com.struts2.RegisterAction">
<result name="success">/success.jsp</result>
<result name="failure">/failure.jsp</result>
</action>
</package>
</struts>
3、在项目下的WebContent目录下新建register.jsp、success.jsp和failure.jsp文件:
register.jsp代码:
<%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd"><html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<form action="register" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="psd"><br>
确认密码:<input type="password" name="repsd"><br>
<input type="submit" value="提交">
<input type="reset">
</form>
</center>
</body>
</html>
success.jsp代码:
<%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd"><html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>注册成功</h1>
</center>
</body>
</html>
failure.jsp代码:
<%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd"><html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>注册失败</h1>
</center>
</body>
</html>
五、运行Web项目
部署好web项目后,在浏览器输入“http://localhost:8080/struts2/register.jsp”,即可运行项目,输入数据测试是否配置好。