日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Struts2中数据封装方式

發(fā)布時(shí)間:2023/12/1 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Struts2中数据封装方式 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、通過ActionContext類獲取

public class ActionContextDemo extends ActionSupport {

?? ?@Override
?? ?public String execute() throws Exception {
?? ??? ?//獲取ActionContext對(duì)象
?? ??? ?ActionContext context = ActionContext.getContext();
?? ??? ?//調(diào)用getParameters對(duì)象獲取參數(shù)
?? ??? ?Map<String, Object> map = context.getParameters();
?? ??? ?//遍歷打印map集合
?? ??? ?for (String key : map.keySet()) {
?? ??? ??? ?String[] val = (String[]) map.get(key);
?? ??? ??? ?System.out.println(key + " : " + Arrays.toString(val));
?? ??? ?}
?? ??? ?return NONE;
?? ?}
}

二、通過ServletActionContext類獲取request類然后獲取

public class ServletActionContextDemo extends ActionSupport {

?? ?@Override
?? ?public String execute() throws Exception {
?? ??? ?//獲取request
?? ??? ?HttpServletRequest request = ServletActionContext.getRequest();
?? ??? ?//獲取參數(shù)
?? ??? ?String username = request.getParameter("username");
?? ??? ?String password = request.getParameter("password");
?? ??? ?String[] hobbies = request.getParameterValues("hobbies");
?? ??? ?System.out.println(username + " : " + password + " : " + Arrays.toString(hobbies));
?? ??? ?
?? ??? ?//操作域?qū)ο?br />?? ??? ?//request域
?? ??? ?HttpServletRequest request2 = ServletActionContext.getRequest();
?? ??? ?request2.setAttribute("request", "hello request");
?? ??? ?
?? ??? ?//獲取session域
?? ??? ?HttpSession session = request2.getSession();
?? ??? ?session.setAttribute("session", "hello session");
?? ??? ?
?? ??? ?//獲取servletcontext域
?? ??? ?ServletContext servletContext = request2.getServletContext();
?? ??? ?servletContext.setAttribute("servletContext", "application");
?? ??? ?return SUCCESS;
?? ?}
?? ?
}

三、屬性封裝

定義私有的成員變量,變量名稱與表單中name屬性值一致

提供成員變量的get和set方法(實(shí)際上,在數(shù)據(jù)封裝時(shí),僅提供set方法即可。成員變量的屬性名也不一定非得跟name屬性值一致,但set方法跟的字段setXXX中的XXX必須跟name屬性名的首字符大寫一致)
public class DataPackagingAction extends ActionSupport {
?? ?private static final long serialVersionUID = 1L;
?? ?private String username;
?? ?private String password;
?? ?private String[] hobbies;
?? ?public String getUsername() {
?? ??? ?return username;
?? ?}
?? ?public void setUsername(String username) {
?? ??? ?this.username = username;
?? ?}
?? ?public String getPassword() {
?? ??? ?return password;
?? ?}
?? ?public void setPassword(String password) {
?? ??? ?this.password = password;
?? ?}
?? ?public String[] getHobbies() {
?? ??? ?return hobbies;
?? ?}
?? ?public void setHobbies(String[] hobbies) {
?? ??? ?this.hobbies = hobbies;
?? ?}
?? ?
?? ?@Override
?? ?public String execute() throws Exception {
?? ??? ?System.out.println("屬性驅(qū)動(dòng):? " + username + " : " + password + "? " + Arrays.toString(hobbies));
?? ??? ?return NONE;
?? ?}
?? ?
}

四、基于模型驅(qū)動(dòng)的數(shù)據(jù)封裝方法

  1.讓action類實(shí)現(xiàn)ModelDriven<T>接口

  2.實(shí)現(xiàn)ModelDriven<T>接口中的getModel方法

  3.在Action中創(chuàng)建私有的成員變量,并手動(dòng)創(chuàng)建實(shí)體類


public class DataPackagingAction2 extends ActionSupport implements ModelDriven<User> {

?? ?private User user = new User();

?? ?@Override
?? ?public User getModel() {
?? ??? ?return user;
?? ?}

?? ?@Override
?? ?public String execute() throws Exception {
?? ??? ?System.out.println(user);
?? ??? ?return NONE;
?? ?}
?? ?
}

五、復(fù)雜數(shù)據(jù)的封裝方法

1.封裝數(shù)據(jù)到list集合中

  第一步: 在action中聲明list成員變量,并手動(dòng)創(chuàng)建實(shí)體類;

  第二部:? 提供get和set方法;

  第三部: 在jsp頁(yè)面中,提供基于list作為值得name屬性

?

public class ListAction extends ActionSupport {
?? ?private List<User> list = new ArrayList<User>();
?? ?public List<User> getList() {
?? ??? ?return list;
?? ?}
?? ?public void setList(List<User> list) {
?? ??? ?this.list = list;
?? ?}??
?? ?@Override
?? ?public String execute() throws Exception {
?? ??? ?for (int i = 0; i < list.size(); i ++) {
?? ??? ??? ?System.out.println(list.get(i));
?? ??? ?}
?? ??? ?return NONE;
?? ?}
}

  在jsp頁(yè)面中name屬性的賦值規(guī)則

??? <form action="${pageContext.request.contextPath}/list.action" method="post">
?? ??? ?username: <input type="text" name="list[0].username" /><br/>
?? ??? ?password: <input type="password" name="list[0].password" /><br/>
?? ??? ?hobby: <input type="checkbox" name="list[0].hobbies" value="basketball" />basketball
?? ??? ?<input type="checkbox" name="list[0].hobbies" value="football" />football
?? ??? ?<input type="checkbox" name="list[0].hobbies" value="badminton" />badminton
?? ??? ?<hr/>
?? ??? ?
?? ??? ?username: <input type="text" name="list[1].username" /><br/>
?? ??? ?password: <input type="password" name="list[1].password" /><br/>
?? ??? ?hobby: <input type="checkbox" name="list[1].hobbies" value="basketball" />basketball
?? ??? ?<input type="checkbox" name="list[1].hobbies" value="football" />football
?? ??? ?<input type="checkbox" name="list[1].hobbies" value="badminton" />badminton
?? ??? ?<hr/>
?? ??? ?
?? ??? ?<input type="submit" value="提交" />
?? ?</form>

2.封裝數(shù)據(jù)到map集合中

?

  第一步: 在action中聲明map成員變量,并手動(dòng)創(chuàng)建實(shí)體類;

?

  第二部:? 提供get和set方法;

  第三部: 在jsp頁(yè)面中,提供基于map作為值得name屬性

public class MapAction extends ActionSupport {
?? ?private Map<String, User> map = new HashMap<String, User>();
?? ?public Map<String, User> getMap() {
?? ??? ?return map;
?? ?}
?? ?public void setMap(Map<String, User> map) {
?? ??? ?this.map = map;
?? ?}
?? ?@Override
?? ?public String execute() throws Exception {
?? ??? ?for (String key : map.keySet()) {
?? ??? ??? ?System.out.println(key + "? " + map.get(key));
?? ??? ?}
?? ??? ?return NONE;
?? ?}
}

在jsp頁(yè)面中name屬性的命名規(guī)則

<form action="${pageContext.request.contextPath}/map.action" method="post">
?? ??? ?username: <input type="text" name="map['one'].username" /><br/>
?? ??? ?password: <input type="password" name="map['one'].password" /><br/>
?? ??? ?hobby: <input type="checkbox" name="map['one'].hobbies" value="basketball" />basketball
?? ??? ?<input type="checkbox" name="map['one'].hobbies" value="football" />football
?? ??? ?<input type="checkbox" name="map['one'].hobbies" value="badminton" />badminton
?? ??? ?<hr/>
?? ??? ?
?? ??? ?username: <input type="text" name="map['two'].username" /><br/>
?? ??? ?password: <input type="password" name="map['two'].password" /><br/>
?? ??? ?hobby: <input type="checkbox" name="map['two'].hobbies" value="basketball" />basketball
?? ??? ?<input type="checkbox" name="map['two'].hobbies" value="football" />football
?? ??? ?<input type="checkbox" name="map['two'].hobbies" value="badminton" />badminton
?? ??? ?<hr/>
?? ??? ?
?? ??? ?<input type="submit" value="提交" />
?? ?</form>

3.使用屬性封裝數(shù)據(jù)到對(duì)象中

  第一步: 在action中聲明實(shí)體類User的成員變量,可以不用實(shí)例化

  第二步: 提供實(shí)體類的get和set方法

  第三部: jsp中name屬性基于實(shí)體類賦值
public class UserAction extends ActionSupport {
?? ?private static final long serialVersionUID = 1L;
?? ?//聲明實(shí)體類
?? ?private User user;
?? ?//生成get和set方法
?? ?public User getUser() {
?? ??? ?return user;
?? ?}
?? ?public void setUser(User user) {
?? ??? ?this.user = user;
?? ?}
?? ?//數(shù)據(jù)打印
?? ?@Override
?? ?public String execute() throws Exception {
?? ??? ?System.out.println("~~~~~~" + user);
?? ??? ?return NONE;
?? ?}
}
jsp頁(yè)面中name屬性的命名規(guī)則

 <form action="${pageContext.request.contextPath}/user.action" method="post">
?? ??? ?username: <input type="text" name="user.username" /><br/>
?? ??? ?password: <input type="password" name="user.password" /><br/>
?? ??? ?hobby: <input type="checkbox" name="user.hobbies" value="basketball" />basketball
?? ??? ?    <input type="checkbox" name="user.hobbies" value="football" />football
?? ??? ?    <input type="checkbox" name="user.hobbies" value="badminton" />badminton
?? ??? ?<hr/>
?? ??? ?<input type="submit" value="提交" />
?? ?</form>

?

轉(zhuǎn)載于:https://www.cnblogs.com/rodge-run/p/6441636.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的Struts2中数据封装方式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。