這篇文章是關於struts2的配置,之前我在網上或者貼吧上都看到很多人在問struts2的配置問題。所以寫這篇來給需要幫助的人,struts2的配置並不複雜。
一.先準備好需要的6個jar包(反正我配置的時候就需要到6個):
1.commons-fileupload-1.2.1.jar
2.commons-io-1.3.2.jar
3.freemarker-2.3.15.jar
4.ognl-2.7.3.jar
5.struts2-core-2.1.8.jar
6.xwork-core-2.1.6.jar
二.新建工程
src:裏新建struts.xml內容如下:
<struts>
<package name="default" namespace="/test" extends="struts-default">
//name類似java的包名字不能重複,namespace爲運行這個包的路徑,extends固
//定寫法
<action name="hello" class="cn.edu.action.helloaction" method="execute">
//name爲action的名字可以重複,但是不建議這樣,class爲調用方法的全部名稱包
//括報名字,method爲方法中要執行的方法名字(本人基礎不是太好,如果看不懂
//請對照下邊代碼看)
<result name="success">/index.jsp</result>
//name爲方法中要返回的結果名稱,這裏注意如果要跳轉到index.jsp需要在前邊
//加/,
</action>
</package>
</struts>
web.xml:內容如下
<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>
直接複製即可
cn.edu.action中的helloaction方法中的代碼:
package cn.edu.action;
public class helloaction {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.aa = message;
}
public String execute(){ //這裏的方法名字對應struts.xml中的method="execute"
this.message="helloword";
return "success"; //這裏對應struts.xml中的result標籤中的name
}
}
index.jsp中
直接輸入${message}就可以打印出helloword(具體原因我還不清楚正在學習中)
三.頁面上輸入
http://localhost/工程名字/struts.xml中你要跳轉action的包的路徑/action的名字
例如http://localhost/struts/test/hello