搭建一個struts2的框架,在之前已經(jīng)搭建過struts的框架了拯坟,這里的流程基本上差不多般贼,詳見 struts1的搭建
首先到官網(wǎng)上下載jar包,這里附一個git的鏈接struts2jar包下載
新建工程骑科,將下載的jar解壓至工程中橡淑,項目結(jié)構(gòu)如下:
項目結(jié)構(gòu)
接下來編寫struts.xml
默認(rèn)加載的配置文件名為struts.xml
private static final String DEFAULT_CONFIGURATION_PATHS = "struts-default.xml,struts-plugin.xml,struts.xml";
此處為Dispatcher中的設(shè)置
如果要默認(rèn)讀取的位置需要在struts2filter中加入
<init-param>
<param-name>filterConfig</param-name>
<param-value>classpath:struts2_demo/struts.xml</param-value>
</init-param>
下面是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是包,是struts提供的對于action的管理方式 -->
<!-- name是package的唯一標(biāo)示符 -->
<!-- extends 繼承 繼承會將被繼承包的action與result都繼承到,而所有包都需要繼承struts-default這個一個包 -->
<!-- struts-default這一個包是struts提供給我們的一個默認(rèn)包,里面包含了很多已經(jīng)定義好的攔截器與result -->
<package name="hello" extends="struts-default">
<!-- action 是指一個具體的動作 -->
<!-- class 指處理這一具體動作的類 -->
<!-- method 指處理該動作的類的方法,默認(rèn)為execute -->
<action name="hi" class="com.education.action.HelloWorldAction"
method="execute">
<!-- result 返回結(jié)果 -->
<!-- name 指執(zhí)行方法的返回值,默認(rèn)為success -->
<!-- result中間的值是最終跳轉(zhuǎn)到的頁面 -->
<!-- result的默認(rèn)跳轉(zhuǎn)方式為轉(zhuǎn)發(fā),如果需要重定向,則增加一個屬性 type="redirect";重定向到某個action type="redirectAction" -->
<result>/success.jsp</result>
<result name="error">/fail.jsp</result>
</action>
</package>
</struts>
struts.xml配置好后需要在web.xml中加入struts的過濾器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>struts2_demo</display-name>
<!-- 配置struts2的過濾器 -->
<filter>
<filter-name>struts2</filter-name>
<!-- 該類可在struts2-core包中找到 -->
<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>
struts配置完畢后建立一個action
package com.education.action;
import com.education.bean.User;
import com.opensymphony.xwork2.ActionSupport;
//action都會繼承一個ActionSupport,單這不是強(qiáng)制的,ActionSupport中包含了很多方法以及常用常量
public class HelloWorldActionextends ActionSupport {
// 前臺傳入的值會直接注入到該Action的屬性中,必須含有g(shù)et/set方法
// 如果是非對象則以 <input name="testMsg"/>這樣的形式傳值
// 如果是對象則以<input name="user.username"/>這樣的形式傳值
private User user;
private String testMsg;
public String getTestMsg() {
return testMsg;
}
public void setTestMsg(String testMsg) {
this.testMsg = testMsg;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
// execute方法是當(dāng)struts.xml沒有指定方法來處理請求時,就會默認(rèn)調(diào)用該方法
@Override
public String execute() throws Exception {
return SUCCESS;
}
// validate方法是struts框架自帶的驗證方法
// 如果重寫了該方法則會先于execute方法執(zhí)行
// 如果運行了addFieldError方法則會直接返回,不再執(zhí)行execute方法
// 返回值為input
@Override
public void validate() {
if (1 > 2) {
addFieldError("name", "it's impossible");
}
}
}
好了咆爽,一個struts2框架的項目就已經(jīng)搭建完畢了,這里就不再發(fā)jsp頁面的代碼了梁棠,剩下的部分就請自己補(bǔ)全吧置森。
這里附上完整項目的下載鏈接
點此下載