1、下載Struts2,http://struts.apache.org/development/2.x/ java
2、解壓獲取包,放到web工程 web
3、在Eclipse中新建Dynamic Web Project工程結構目錄以下 apache
四,java代碼以下 app
com.lianghao jsp
public class HelloWorld { this
private String message; url
/**
* sturts2 Action默認執行的方法
*
* @return
*/
public String execute() {
System.out.println("第一個Struts2應用!");
this.setMessage("第一個Struts2應用!");
return "success";
} spa
public String getMessage() {
return message;
} .net
public void setMessage(String message) {
this.message = message;
} xml
}
五,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>
6、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>