Structs2 ModelDriven使用方法介绍
生活随笔
收集整理的這篇文章主要介紹了
Structs2 ModelDriven使用方法介绍
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
例子都為struts2中的文檔例子
| JSP提交數(shù)據(jù): <s:form action="modelDrivenResult"?method="POST"?namespace="/modelDriven">?? ????<s:textfield label="Gangster Name"?name="name"?/> ????<s:textfield label="Gangster Age"??name="age"?/> ????<s:checkbox? label="Gangster Busted Before"?name="bustedBefore"?/> ????<s:textarea? cols="30"?rows="5"?label="Gangster Description"?name="description"?/>?????????? ????<s:submit /> </s:form> |
?根據(jù)數(shù)據(jù)提交的action,和namespace,修改struts.xml的內(nèi)容
| 添加下面配置到struts.xml ????<package?name="base2"?namespace="/modelDriven"?extends="struts-default"> ????????<action name="modelDrivenResult"?class="ModelDrivenAction"> ????????????<result>/Hello.jsp</result> ????????</action> ????</package> |
然后添加處理數(shù)據(jù)的action,這里使用ModelDriven的方式。該action繼承ModelDriven接口,實現(xiàn)getModel()方法,方法只返回一個模型對象,默認轉(zhuǎn)換Object。只要改模型對象提供的方法和提交數(shù)據(jù)的name屬性對應,struts2就能自動把數(shù)據(jù)賦給模型對象,最后對象提供各種get方法把數(shù)據(jù)存放在值棧,傳遞到Hello.jsp。
| 處理的action,返回模型對象Gangster。 public?class?ModelDrivenAction extends?ActionSupport implements?ModelDriven { ????public?String execute() throws?Exception { ????????return?SUCCESS; ????} ????public?Object getModel() { ????????return?new?Gangster(); ????} } |
?
| 模型類 public?class?Gangster implements?Serializable { ????private?String name; ????private?int?age; ????private?String description; ????private?boolean?bustedBefore; ????public?int?getAge() { ????????return?age; ????} ????public?void?setAge(int?age) { ????????this.age = age; ????} ????public?boolean?isBustedBefore() { ????????return?bustedBefore; ????} ????public?void?setBustedBefore(boolean?bustedBefore) { ????????this.bustedBefore = bustedBefore; ????} ????public?String getDescription() { ????????return?description; ????} ????public?void?setDescription(String description) { ????????this.description = description; ????} ????public?String getName() { ????????return?name; ????} ????public?void?setName(String name) { ????????this.name = name; ????} } |
?最后Hello.jsp中可以使用<s:property value=".."/>取值,value直接使用相應的age,name等。而action雖然提供的getModel()方法,但因為轉(zhuǎn)為了Object,而Object不能提供取值的方法,所以無法使用value="model.xxx"來取值。
轉(zhuǎn)載于:https://www.cnblogs.com/lechance/p/4373288.html
總結
以上是生活随笔為你收集整理的Structs2 ModelDriven使用方法介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: struct ethhdr结构体详解
- 下一篇: 如何使Putty会话颜色更美观