更新時間:2021-07-30 16:50:42 來源:動力節(jié)點 瀏覽1025次
雖然說Struts2現(xiàn)在已經(jīng)被SpringMVC框架淘汰了,據(jù)說是有很多安全漏洞。但是Struts2作為一個成熟的MVC框架,還是有必要了解一下的,好歹是曾經(jīng)風(fēng)光一時的前輩,老祖宗的東西不能丟下,里面有很多設(shè)計思想都是值得借鑒的。
哈哈哈,前面說的話逼格是不是很高,連我自己都不信。其實是因為最近在維護(hù)SSH(Struts2+Spring+Hibernate)框架寫的老項目啦,其中有些代碼甚至可以追溯到2004年,簡直666。雖然是年代久遠(yuǎn)的祖?zhèn)鞔a,很不愿意維護(hù),但是工作(恰飯)要緊,還是要稍微學(xué)習(xí)一下Struts2的使用,輕裝上陣。
Struts2編寫Action的三種方式
1.創(chuàng)建普通類。
自己創(chuàng)建的普通類需要人為定義常量SUCCESS,而且沒有表單校驗等功能,需要自己編寫代碼實現(xiàn)。
Struts2編寫Action的三種方式
1.創(chuàng)建普通類。
自己創(chuàng)建的普通類需要人為定義常量SUCCESS,而且沒有表單校驗等功能,需要自己編寫代碼實現(xiàn)。
package com.test.struts2;
public class TestAction {
public String execute() {
return "success";
}
public String add() {
return "success";
}
public String delete() {
return "error";
}
}
2.實現(xiàn)Action接口。
Action接口中定義了常量SUCCESS。
package com.test.struts2;
import com.opensymphony.xwork2.Action;
public class TestAction1 implements Action {
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
public String add() {
// System.out.println("我是add()");
return SUCCESS;
}
}
3.繼承ActionSupport類(最常用)。
ActionSupport類其實是實現(xiàn)了Action接口的,其中封裝的功能較多,實現(xiàn)了表單驗證及常量SUCCESS的定義等。
package com.test.struts2;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction2 extends ActionSupport {
public String execute() {
return SUCCESS;
}
}
Struts2訪問Action的三種方式
1.訪問action默認(rèn)調(diào)用execute()方法。比如說有一個TestAction2,訪問localhost:8080/test2就會調(diào)用execute()方法。如果要調(diào)用action中的其他方法,需要使用【!】符號,比如另外有一個laugh()方法,就可以通過訪問localhost:8080/test2!laugh進(jìn)行調(diào)用。
<!-- 繼承ActionSupport類,默認(rèn)的method調(diào)用execute() -->
<package name="test2" extends="struts-default" namespace="/test2">
<action name="test2" class="com.test.struts2.TestAction2">
<result>/test2.jsp</result>
</action>
</package>
2.在action標(biāo)簽中配置method屬性。
<!-- 實現(xiàn)Action接口,method屬性實現(xiàn)請求 -->
<package name="test1" extends="struts-default" namespace="/test1">
<action name="test1" class="com.test.struts2.TestAction1" method="add">
<result>/test1.jsp</result>
</action>
</package>
3.通配符訪問。比如localhost:8080/test_方法名。
<!-- 普通類實現(xiàn)Action接口,method用通配符實現(xiàn)請求 -->
<package name="test" extends="struts-default" namespace="/test">
<action name="test_*" class="com.test.struts2.TestAction" method="{1}">
<result name="success">/test1.jsp</result>
<result name="error">/test2.jsp</result>
</action>
</package>
Struts2獲取前臺參數(shù)的三種方式
這里使用form表單提交作為例子,當(dāng)然使用ajax提交都可以。
1.屬性驅(qū)動。
在Action中定義屬性,確保所定義的屬性名稱與Form表單中的name屬性一致,適用于屬性個數(shù)較少的情況。
<form action="login" method="post>
用戶名:<input type="text" name="username"/><br/>
密碼:<input type="password" name="password"/><br/>
<button type="submit" name="login">登錄</button>
</form>
public class LoginAction extends ActionSupport {
// 屬性驅(qū)動,需要input中name屬性值相同(屬性個數(shù)較少情況)
private String username;
private String password;
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String login() {
if ("yanggb".equals(username) && "123".equals(password)) {
return SUCCESS;
}
}
}
2.對象驅(qū)動。
創(chuàng)建一個對象實例,將表單定義的屬性匹配給所定義的對象,即以對象為單位,用對象獲取屬性,方便屬性較多的情況。
<form action="login" method="post>
用戶名:<input type="text" name="user.username"/><br/>
密碼:<input type="password" name="user.password"/><br/>
<button type="submit" name="login">登錄</button>
</form>
public class LoginAction extends ActionSupport {
// 對象驅(qū)動,input中name屬性要以【對象.屬性】格式組織
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String login() {
if ("yanggb".equals(user.username) && "123".equals(user.password)) {
return SUCCESS;
}
}
}
3.模型驅(qū)動。
模型驅(qū)動是對象驅(qū)動的升級,實現(xiàn)ModelDriven<User>接口,人為將一個對象User作為一個Model,將View層和Controller層關(guān)聯(lián)起來。這樣,View就能實現(xiàn)表單屬性的匹配,同時也能知道Controller層的功能。只要使用getModel()返回對象user匹配model中的屬性即可,頁面不需要對象。
<form action="login" method="post>
用戶名:<input type="text" name="username"/><br/>
密碼:<input type="password" name="password"/><br/>
<button type="submit" name="login">登錄</button>
</form>
public class LoginAction extends ActionSupport implements ModelDriven<User> {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String login() {
if ("yanggb".equals(user.username) && "123".equals(user.password)) {
return SUCCESS;
}
}
@override
public User getModel() {
// TODO Auto-generated method stub
// 得到model對象user(自動匹配model里面的屬性,不用再在form的name屬性中寫具體對象)
return user;
}
}
要注意的是,當(dāng)屬性驅(qū)動與模型驅(qū)動同時存在的時候,不會執(zhí)行屬性驅(qū)動方法,而是直接執(zhí)行模型驅(qū)動來獲取參數(shù)。
以上就是動力節(jié)點小編介紹的"struts2使用入門",希望對大家有幫助,想了解更多可查看Struts2視頻教程。動力節(jié)點在線學(xué)習(xí)教程,針對沒有任何Java基礎(chǔ)的讀者學(xué)習(xí),讓你從入門到精通,主要介紹了一些Java基礎(chǔ)的核心知識,讓同學(xué)們更好更方便的學(xué)習(xí)和了解Java編程,感興趣的同學(xué)可以關(guān)注一下。
初級 202925
初級 203221
初級 202629
初級 203743