Struts 2 with Ajax
StrutsandStruts2comparedtoamajorimprovementissupportforAjax.Inthispaper,lookattheDivinStruts2ishowAjaxusedtooutputtheresults,themainuseoftheDojo.
Firstofall,wefirstcreateasimpleusecase,inthisusecasewillbedisplayedonthescreenofauserlist,clickontheuseridlist,thelistbelowwillshowtheuserdetailedinformation,showingdetailedinformationoftheuserstepswewilltheuseofAjax.
First,createaweb.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="2.5"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Second,createstruts.xml
<!DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<packagename="ajaxdemo"extends="struts-default">
<actionname="UserListingAction">
<result>/userlisting.jsp</result>
</action>
<actionname="UserDetailAction">
<result>/userdetail.jsp</result>
</action>
</package>
</struts>
III,page:userlisting.jsp
Displayslistofusers
<%@Taglibprefix="s"uri="/struts-tags"%>
<html>
<head>
<s:headtheme="ajax"/>
</head>
<script>
functionshow_user_details(id)(
document.frm_user.userid.value=id;
dojo.event.topic.publish("show_detail");
)
</script>
<body>
<s:formname="frm_user">
<h1>UserListing</h1>
<s:iftest="userList.size>0">
<tableborder="1">
<s:iteratorvalue="userList">
<tr>
<td>
<s:ahref="#"onclick="javascript:show_user_details('%{id}');returnfalse;"><s:propertyvalue="id"/></s:a>
</td>
<td>
<s:propertyvalue="name"/>
</td>
</tr>
</s:iterator>
</table>
</s:if>
<s:hiddenname="userid"/>
<s:urlaction="UserDetailAction"/>
<s:divhref="%{d_url}"theme="ajax"listenTopics="show_detail"formid="frm_user">
</s:div>
</s:form>
</body>
</html>
IV,page:userdetail.jsp,usedtodisplayuserdetailsfromuserlisting.jspload
<%@Taglibprefix="s"uri="/struts-tags"%>
<h1>UserDetails</h1>
<s:iftest="userDetails!=null">
<table>
<tr><td>Id:</td><td><s:propertyvalue="userDetails.id"/></td></tr>
<tr><td>Name:</td><td><s:propertyvalue="userDetails.name"/></td></tr>
<tr><td>Email:</td><td><s:propertyvalue="userDetails.email"/></td></tr>
<tr><td>Address:</td><td><s:propertyvalue="userDetails.address"/></td></tr>
</table>
</s:if>
5,ajaxdemo.action.UserListingAction.java,togeneratealistofdatausersbyuserlisting.jspshowthatinpracticalapplications,thispartofthedataaregenerallyobtainedfromthedatabase.
packageajaxdemo.action;
importajaxdemo.dto.UserListDTO;
importcom.opensymphony.xwork2.ActionSupport;
importjava.util.ArrayList;
importjava.util.List;
/***//**Populatestheuserlistingdata*/
publicclassUserListingActionextendsActionSupport(
privateList<UserListDTO>userList;//thisisavailableinviewautomatically!
publicStringexecute()throwsException(
//Create2userobjectsandaddtoalist
setUserList((List<UserListDTO>)newArrayList());
UserListDTOuser=newUserListDTO();
user.setId("gjose");
user.setName("GraceJoseph");
getUserList().add(user);
user=newUserListDTO();
user.setId("peter");
user.setName("PeterSmith");
getUserList().add(user);
returnSUCCESS;
)
publicList<UserListDTO>getUserList()(
returnuserList;
)
publicvoidsetUserList(List<UserListDTO>userList)(
this.userList=userList;
)
)
6,ajaxdemo.action.UserDetailAction.java,whentheuseridisselected,theuseraccesstodetailedinformationthroughthedojotocall.
packageajaxdemo.action;
importajaxdemo.dto.UserDetailDTO;
importcom.opensymphony.xwork2.ActionSupport;
/**//*Populatesuserdetailsforauseridselected*/
publicclassUserDetailActionextendsActionSupport(
privateStringuserid;
privateUserDetailDTOuserDetails;
publicStringexecute()throwsException(
//Populateonlywhenuseridisselected
if(userid!=null&&!userid.equals(""))
populateDetail(userid);
returnSUCCESS;
)
privatevoidpopulateDetail(Stringid)(
userDetails=newUserDetailDTO();
userDetails.setId(id);
userDetails.setName("TheCompleteName");
userDetails.setEmail("[email protected]");
userDetails.setAddress("richstreet,lavishroad,StrutsLand");
)
publicStringgetUserid()(
returnuserid;
)
publicvoidsetUserid(Stringuserid)(
this.userid=userid;
)
publicUserDetailDTOgetUserDetails()(
returnuserDetails;
)
publicvoidsetUserDetails(UserDetailDTOuserDetails)(
this.userDetails=userDetails;
)
)
7,ajaxdemo.action.UserDetailDTO.java,POJO,userinformationforthepackage
packageajaxdemo.dto;
publicclassUserDetailDTO(
privateStringid;
privateStringname;
privateStringemail;
privateStringaddress;
publicStringgetId()(
returnid;
)
publicvoidsetId(Stringid)(
this.id=id;
)
publicStringgetName()(
returnname;
)
publicvoidsetName(Stringname)(
this.name=name;
)
publicStringgetEmail()(
returnemail;
)
publicvoidsetEmail(Stringemail)(
this.email=email;
)
publicStringgetAddress()(
returnaddress;
)
publicvoidsetAddress(Stringaddress)(
this.address=address;
)
)
OK,deployedaftertheinputtotesthttp://localhost:8080/ajaxdemo/UserListingAction.action.
Whenthelistofuseridtobeafterthepoint,javaScriptnoticeDivtagfromtheURLtoreturntothecontentsofdynamicloading.Thisexample,UserDetailActionmadeandpassedtotheuser'sinformationuserdetail.jsp.userdetail.jsptogeneratethefinaldisplayofresults,showingintheDiv.
相关推荐
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo