1、eclipse中新建一个dynamic web project工程sturts2,将struts2的架包复制到WEB-INF中的lib文件中,架包有:commons-fileupload-1.3.1.jar,commons-io-2.2.jar,commons-lang3-3.1.jar,commons-logging-1.1.3.jar,core-0.6.2.jar,freemarker-2.3.19.jar,javassist-3.11.0.GA.jar,ognl-3.0.6.jar,servlet-api.jar(此包在tomcat里),struts2-core-2.3.16.3.jar,xwork-core-2.3.16.3.jar。
2、在WEB-INF中建立web.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns="java.sun.com/xml/ns/javaee" xsi:schemaLocation="java.sun.com/xml/ns/javaee java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<!-- <display-name>Struts2</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>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>
</web-app>
3、在src中建立struts.xml 内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 配置包,名称为bookcode -->
<package name="" extends='struts-default' namespace="/test">
<!-- 配置Action -->
<action name="helloworld" class="cn.xs.action.HelloWorldAction" method = "execute">
<!-- 配置返回结果 -->
<result name="success">/WEB-INF/page/hello.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
4、在src中建立包cn.xs.action,包中建立HelloWorldAction.java
// ---------------------------------------------------------
// @author sheng.xu
// @version 1.0.0
// @date 2014年5月29日
// ---------------------------------------------------------
package cn.xs.action;
/**
* @author sheng.xu
*
*/
public class HelloWorldAction {
public String message;
/**
* @return the message
*/
public String getMessage() {
return message;
}
/**
* @param message the message to set
*/
public void setMessage(String message) {
this.message = message;
}
public String execute(){
this.message = "aaaaaa";
return "success";
}
}
5、在WEB-INF中建立文件夹page,在page中建立hello.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" "www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${message}
</body>
</html>
6、导出war包,放入tomcat的webapp目录中
7、浏览器输入:localhost:8080/Struts2/test/helloworld