1,官网http://struts.apache.org/download.cgi#struts下载struts2, 解压,找到struts-2.5.13-all\struts-2.5.13\apps\下的struts2-rest-showcase进行解压,后所需的jar包在解压后的struts2-rest-showcase\WEB-INF\lib目录下 (根据自己下的版本而定一般结构差不多)主要引用以下包:
commons-fileupload-1.3.3.jar
commons-io-2.5.jar
commons-lang-2.4.jar
commons-lang3-3.6.jar
commons-logging-1.1.3.jar
freemarker-2.3.23.jar
javassist-3.20.0-GA.jar
log4j-api-2.8.2.jar
log4j-core-2.8.2.jar
ognl-3.1.15.jar
struts2-core-2.5.13.jar
2,新建java web工程
(1)将其导入到WebContent/WEB-INF/lib下然后右键build path add jar即可
(2)也可以使用maven引用远程jar包,个人偏向使用maven引用jar(不需要自己去官网下载jar包)具体使用请查看maven javaweb的使用在pom.xml下配置如下:
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts2.version}</version>
<scope>runtime</scope>
</dependency>
3,在WebContent/WEB-INF的web下配置如下
<!-- 配置Struts2的核心过滤器 -->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<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>
4,新建login.jsp如下:
<form action="login" method="post">
<label for="usermanelabel" >用户名:</label>
<inputtype="text" name="username" id="usermanelabel" />
<s:fielderror>
<s:param>username</s:param>
</s:fielderror>
<div>
<label for="usermanelabel">密码:</label>
<inputtype="text" name="userpwd" id="usermanelabel" />
<s:fielderror>
<s:param>userpwd</s:param>
</s:fielderror>
</s:fielderror>
<s:fielderror>
<s:param>query</s:param>
</s:fielderror>
</div>
<div >
<input type="submit" value="登录" />
</div>
</form>
* action="login" 为struts.xml中的action,下面有介绍
5,新建UserAction
public class LoginAction extends ActionSupport {
private String username;
private String userpwd;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
@Override
public String execute() throws Exception {
if (userpwd.equals("123")&&username.equals("123")) {
return "success";
} else {
addFieldError("query", "该用户不存在!!!");
return "input";
}
}
@Override
public void validate() {
// TODO Auto-generated method stub
super.validate();
if (StringUtils.isEmpty(username)) {
addFieldError("username", "用户名不能为空");
} else if (StringUtils.isEmpty(userpwd)) {
}
}
}
* 执行LoginAction 时先执行validate()校验一下,利用addFieldError,将错误信息在login.js中利用<s:fielderror>输出错误信息。
* oginAction默认执行execute() 方法,Struts2的配置文件struts.xml会根据返回值进行页面跳转.
* 如果addFieldError后会返回"input"
5,在WebContent/WEB-INF下创建class在class下创建struts.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- <constant name="struts.ui.theme" value="simple"></constant> -->
<constant name="struts.devMode" value="true" />
<package name="user" extends="struts-default">
<action name="login" class="com.test.action.LoginAction><!-- method="result" -->
<result name="success" type="dispatcher">
<param name="location">/index.jsp</param>
</result>
<result name="failed">/error.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
* loginuser为login.jsp中action对应的值,com.test.action.LoginAction为对应的类, success,failed,input则是LoginAction中返回的值,根据返回值跳转到相应界面