一、下载Struts2,http://struts.apache.org/development/2.x/
二、解压获取包,放到web工程
三、在Eclipse中新建Dynamic Web Project工程结构目录如下
四,java代码如下
com.lianghao
public class HelloWorld {
private String message;
/**
* sturts2 Action默认执行的方法
*
* @return
*/
public String execute() {
System.out.println("第一个Struts2应用!");
this.setMessage("第一个Struts2应用!");
return "success";
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
五,web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<!-- 下面是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>
<!-- Struts2配置完成 -->
</web-app>
六、struts.xml配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="demo" namespace="/" extends="struts-default">
<action name="HelloWorld" class="com.lianghao.HelloWorld" >
<result name="success">/helloWorld.jsp</result>
</action>
</package>
</struts>