国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Struts2 Hibernate開發(fā)筆記(一) - Java企業(yè)開發(fā)
truts2 + Hibernate開發(fā)筆記(一)

由于開發(fā)任務(wù)緊張,因為這里的開發(fā)筆記,僅用于記錄遇到的幾個struts2和hibernate結(jié)合開發(fā)的現(xiàn)象.不對其做分析.
1.        在使用struts2時,頁面和action之間的傳值
這是struts2和struts1最大的區(qū)別.
Struts2中,action和jsp頁面之間的信息交互,是通過 action中定義的成員變量來實現(xiàn)的.
例如,我在一個名為EstateAction的類中有如下定義
public class CityAction extends BaseAction {
        private MthCity mthCity ;
        private String cityName;
        private Long cityId
        private int couter;

        public String loadCity() throws DataAccessException, BaseException{
        counter ++;
                return "city";
        }
}
然后,這里上面的類中的成員類MthCity的定義如下
public class MthCity implements java.io.Serializable {
       
        private Long cityId       
        private String cityName;
        public MthCity() {
        public Long getCityId() {
                return this.cityId;
        }
        public void setCityId(Long cityId) {
                this.cityId = cityId;
        public String getCityName() {
                return this.cityName;
        }
        public void setCityName(String cityName) {
                        this.cityName = cityName;
        }
}
這是一個Hibatenate使用的數(shù)據(jù)對象 POJO類.
有了這兩個類后,我們來看看Struts2的Action和JSP頁面之間是如何交互的
一.        JSP -> Action
Jsp頁面
        以下是一個jsp頁面submit.jsp.這個頁面只有一個功能,就是向struts提交申請
<%@ page language="java" contentType="text/html; charset=gbk"%>
<%@ include file="/common/taglibs.jsp"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>提交</title>
</head>
<script>
    function go (){
      window.location ="${pageContext.request.contextPath}/admin/city/loadCity.do”;
  }
</script>
<body>
<form name=”myform” >
  <input type="button" name="cityupdate" id="cityupdate" value="編輯" onclick="javascript:go();"/>
  <input type="hidden" name="mthCity.cityName" id=" mthCity " value="廣州" />

</form>
</body>
</html>

大家可以看到,這個頁面只有一個按鈕,將頁面提交到struts的一個action中,這是為什么呢.
        我們先看一段struts2的配置文件admin-action.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
        <package name="admin" namespace="/admin" extends="struts-default">
                <action name="city/*" method="{1}"
                class="com.mytophome.admin.representation.struts.action.CityAction">
                <result name="city">/admin/city.jsp</result>
                <result name="city_update">/admin/city_update.jsp</result>
                </action>
        </package>
</struts>

這是一個struts2的典型配置文件.
上面有幾處要注意的
首先是namespace = “/admin” 這是一個struts模塊名,可以不寫,但如果寫了,能比較方便的將struts2的action按配置來分模塊.(何謂分模塊呢?struts2有個特性,是action定義不需要像struts1一樣寫在同一個struts.xml文件中.而是可以使用include的形式.例如我使用的項目的struts.xml文件就是這樣寫的:
<struts>
    <include file="struts-action/admin-action.xml"/>
        <include file="struts-action/agent-action.xml"/>
</struts>
這樣include了一系統(tǒng)的xml配置,而上面的admin-action.xml文件就是其中一段,因此將這一段中涉及的action類設(shè)定為一個模塊,就定namespace = “/admin”
)
其次
<action name="city/*" method="{1}"
這一句配置的意思,就是,當(dāng)用戶提交一個符合struts2格式的申請時(所有包含.do形式的http鏈接)
例如http://localhost/admin/city/loadCity.do
其中包含了/city/那么在配置 文件中,只要定義action name=”city/*”,那么所有包含有/city/的do,都會提交到action定義的類中來,也就是類om.mytophome.admin.representation.struts.action.CityAction中,那么提交到這個類的哪個方法中呢? 因為選擇的是city/*.而且mothed={1},所以方法名由鏈接指定
也就是loadCity.do所指定的.loadCity方法.

        這個do方法后面是可以帶參數(shù)的.所帶的參數(shù)名,要是CityAction中定義的成員變量,包括成員類.例如,如果想提交后,CityAction中的cityId有值,鏈接可以這樣寫
http://localhost/admin/city/loadCity.do?cityId=9
這樣,在loadCity方法中,如果你訪問cityId,就可以發(fā)現(xiàn)cityId的值是9
System.out.println(Long.toString(cityId));

但這里有一個條件,就是CityAction中,必須要有cityId變量的getter/setter方法(這兩個方法可以用MyEclipse自動生成)
        public Long getCityId() {
                return cityId;
        }

        public void setCityId(Long cityId) {
                this.cityId = cityId;
        }如果要給CityAction中的MthCity類的
這樣才能在jsp頁面提交時,由struts為cityId賦值.(當(dāng)然,getter方法就方便當(dāng)action返回到j(luò)sp頁面時,cityId的值能在頁面取到.)
        如果要為action中的類成員變量賦值也是可以的
例如http://localhost/admin/city/load ... mp;mthCity.cityId=8
這條鏈接提交后,會和上面一樣調(diào)用CityAction的loadCity方法,但這里,action的成員mthCity會被創(chuàng)建實例,并為mthCity的一個屬性cityId賦值,也就是說,在action中,大家可以通過mthCity.getCityId()來獲得這個屬性的值.
當(dāng)然,一定要在action設(shè)置mthCity的getter setter,jsp上的內(nèi)容才能傳到action中
        public MthCity getMthCity()
                                return mthCity;
        }

        public void setMthCity(MthCity city) {
                this. mthCity = city;
        }


        從JSP提交內(nèi)容到Action,還有一種方法,就是將參數(shù)內(nèi)容放在表單form的元素中
  <input type="hidden" name="mthCity.cityName" id=" mthCity " value="廣州" />

這樣,當(dāng)用以下語句表單提交的時候
doucment. myform.submit();
就能在Action中創(chuàng)建一個mthCity實例,并為mthCity.cityName設(shè)置值為:廣州.
原因是在頁面的表單元素中,設(shè)置了name= mthCity.cityName,而action中剛好有成員類叫mthCity,而這個類中剛好有屬性叫cityName.,就是通過這樣的方法,能將表單的內(nèi)容,提交到Action中.


__________________
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
struts2 示例
普通html標(biāo)簽<form>和struts2<s:form>的區(qū)別
標(biāo)簽package中的namspace的用法
如何將WEB-INF下的jsp頁面插入在frameset中
MyEclipse + struts + Hibernate配置開發(fā)手冊
Struts2.1.6+Spring2.5.6+Hibernate3.3.1全注解實例詳解(四)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服