1.首先下载Struts-2.5.16;
2.解压缩Struts-2.5.16,打开其lib文件夹,将下列基础jar包复制到eclipse新建的工程的web-INF/lib里。
commons-io-2.5.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
ognl-3.1.15.jar
struts2-core-2.5.13.jar
<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>firstSSH</display-name>
<welcome-file-list>
<welcome-file>index.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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="logins.action" method="post">
name:<input type="text" name="userName">
<br>
passWord:<input type="passWord" name="passWord">
<input type="submit" value="提交">
</form>
</body>
</html>
6.创建index2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
${userName}
${passWord }
</body>
</html>
7.在工程的src目录下创建包名com.hanpeng.action,然后在其下创建类LoginAction.java
package com.hanpeng.action;
public class LoginAction {
private String userName;
private String passWord;
public LoginAction() {
// TODO Auto-generated constructor stub
}
//定义Struts默认的方法
public String execute(){
//返回字符串类型给struts.xml文件接收
return "success";
}
//定义错误时应调用方法
public String error(){
//返回字符串类型给struts.xml文件接收
return "error";
}
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;
}
}
8.在工程的src目录下创建struts.xml
<?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>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="rest-showcase" extends="struts-default">
<global-allowed-methods>index,show,create,update,destroy,deleteConfirm,edit,editNew</global-allowed-methods>
<action name="logins" class="com.hanpeng.action.LoginAction">
<result name="success">/index2.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
9.好了现在运行项目,这样Demo就完成了。