正文索引[ 隱藏 ]
★Struts2開發(fā)步驟
1.加載struts2類庫
2.配置web.xml
3.開發(fā)視圖層頁面--hello.jsp
4.開發(fā)控制層Action--HelloAction.java
5.配置struts.xml
6.部署、運行項目。
★錯誤歸納
1.struts.xml的位置問題。
2.Action接受參數(shù)的問題。
3.There is no Action mapped for namespace / and action name UserAction
4. ognl.OgnlException: target is null for setProperty(null, "name", [Ljava.lang.String;@346633b5)
★Struts2開發(fā)步驟
最生動的莫過于實例,看代碼是學(xué)習(xí)編程的最好途徑。
1.加載struts2類庫
【點擊下載】,復(fù)制到/WEB-INF/lib下。
2.配置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>
3.開發(fā)視圖層頁面--hello.jsp
<h1>Welcom!</h1>
<div>
<h1>
<!-- 顯示Struts Action中message屬性內(nèi)容 -->
${message}
</h1>
</div>
<hr />
<div>
<form action="HelloGo.action">
請輸入您的姓名:
<input name="user.name" type="text" />
<input type="submit" value="提交" />
</form>
</div>
【注意】:12行中使用user.name是因為我把name封裝在類UserInfo里,調(diào)用時必須加上對象名user。
4.開發(fā)控制層Action--HelloAction.java
public class HelloAction {
private String message;
private UserIf user;
...//message和user的set,get方法
public String execute() {
this.setMessage("你好,"+this.user.getName()+"!");
return "success";
}
}
【注意】:此處的user必須是和hello.jsp頁面中的name的屬性前綴相一致的。如果改變,則需要兩者同時改變,并且特別注意一點:Action類里的封裝類的set和get方法千萬不要忘記更新,不要只顧著修改變量名稱,而忽略了set、get方法名。我就是經(jīng)常忘記修改set、get方法名,而導(dǎo)致出現(xiàn)一個錯誤ognl.OgnlException: target is null for setProperty(null, "h.name", [Ljava.lang.String;@346633b5),后面再說。
5.配置struts.xml
首先,如果沒有struts.xml,則要在src目錄下新建。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"
http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="jzl" extends="struts-default">
<action name="HelloGo" class="com.action.HelloAction">
<result name="success">hello.jsp</result>
</action>
</package>
</struts>
【注意】:
⑴dtd文件的引入,可以從struts2-core-xxxx.jar包里找到dtd文件,打開后,找到里面的引用粘貼過來即可。我這里用的是struts-2.1.7.dtd。
⑵說一說這里面的一些對應(yīng)名:①<action>里HelloGo是與jsp前臺頁面中<form>中action屬性的名稱相同的。②class的值是前臺頁面所要轉(zhuǎn)向的Action類,記得在沒有用Spring的時候要帶著類的全名。③<result>中success是和Action類中默認方法execute的返回值相同的。
⑶整個配置的含義:配置一個包,package名為“XXX”,必須繼承“struts-default”,否則調(diào)用不到struts提供的interceptor和result節(jié)點等等。把JSP頁面中的action轉(zhuǎn)向到Action類里,例如把名為HelloGo.action轉(zhuǎn)向到com.action.HelloAction類,這個過程同時把數(shù)據(jù)封裝了傳遞過來。當(dāng)處理完畢后,Action類中execute方法返回值為設(shè)定的匹配字符(例如"success")時,將把頁面轉(zhuǎn)向到某一個頁面(例如hello.jsp)。
6.部署、運行項目。
根據(jù)異常來判斷錯誤,下邊就說說我碰到過的各種錯誤。
★錯誤歸納
1.struts.xml的位置問題。
struts.xml要放到src/下,這個得從struts2的控制器org.apache.struts2.dispatcher.FilterDispatcher上說,這個類默認的是導(dǎo)入/classes 下的struts.xml,所以只有放在src/下編譯才能在/classes下,不過可以通過修改web.xml,在<filter>標(biāo)簽內(nèi)加入如下代碼,這樣struts.xml就可以放在/WEB-INF下了。
<init-param>
<param-name>config</param-name>
<!-- 配置裝載struts.xml路徑,其中struts.xml放在/src/struts/下-->
<param-value>struts-default.xml,struts-plugin.xml,../struts.xml</param-value>
</init-param>
2.Action接受參數(shù)的問題。
Struts2中Action接收參數(shù)的方法主要有以下三種:
(1).使用Action的屬性接收參數(shù):
a.定義:在Action類中定義屬性,創(chuàng)建get和set方法;
b.接收:通過屬性接收參數(shù),如:userName;
c.發(fā)送:使用屬性名傳遞參數(shù),如:user1!add?userName=Magci;
(2).使用DomainModel接收參數(shù):
a.定義:定義Model類,在Action中定義Model類的對象(不需要new),創(chuàng)建該對象的get和set方法;
b.接收:通過對象的屬性接收參數(shù),如:user.getUserName();
c.發(fā)送:使用對象的屬性傳遞參數(shù),如:user2!add?user.userName=MGC;
(3).使用ModelDriven接收參數(shù):
a.定義:Action實現(xiàn)ModelDriven泛型接口,定義Model類的對象(必須new),通過getModel方法返回該對象;
b.接收:通過對象的屬性接收參數(shù),如:user.getUserName();
c.發(fā)送:直接使用屬性名傳遞參數(shù),如:user2!add?userName=MGC
我們平時用的最多的就是第二種方式,即創(chuàng)建單獨的entity類,action類里聲明entity類的對象,帶有g(shù)et、set方法,接受的時候用對象來調(diào)用,jsp發(fā)送數(shù)據(jù)的時候用同名的對象名做前綴調(diào)用。
此部分更詳細說明請看:struts2中action接收參數(shù)的方法
3.There is no Action mapped for namespace / and action name UserAction
找不到與之相匹配的Action,一般來說這個錯誤原因有兩種:
(1)檢查struts.xml是否在src目錄下。
(2)檢查struts.xml語法是否正確。如果(1)沒問題,則肯定是在語法問題上。比如extends="struts-default"是否寫錯了,不要忽略,小編當(dāng)時就是寫錯了default詞語;查看文件名是不是struts.xml;根據(jù)上述5. 配置struts.xml下(2)內(nèi)容自行對照各名稱是否相同。
4. ognl.OgnlException: target is null for setProperty(null, "name", [Ljava.lang.String;@346633b5)
這是個關(guān)于Jsp和Action傳遞參數(shù)時的錯誤。檢查的文件有jsp頁面文件、Action類。
(1)檢查jsp頁面中的name屬性是否正確,如果是按封裝類傳遞參數(shù),則檢查前綴是否和Action類中聲明的對象名相同。
(2)如果(1)沒有問題,則檢查Action類中的聲明對象有沒有添加set和get方法。同時檢查一下set和get方法的語法是否正確。
……
以上就是小編通過做一個簡單實例,學(xué)習(xí)到的struts使用方法和錯誤總結(jié),更加深入的學(xué)習(xí)還會繼續(xù)。
原文鏈接:
http://www.itobin.info/iq/javac/struts2.html