当前位置: 代码迷 >> Ajax >> Struts 二 with Ajax
  详细解决方案

Struts 二 with Ajax

热度:260   发布时间:2012-10-07 17:28:51.0
Struts 2 with Ajax
  Struts and Struts2 compared to a major improvement is support for Ajax. In this paper, look at the Div in Struts2 is how Ajax used to output the results, the main use of the Dojo.

First of all, we first create a simple use case, in this use case will be displayed on the screen of a user list, click on the userid list, the list below will show the user detailed information, showing detailed information of the user steps we will the use of Ajax.

First, create a web.xml

<? xml version = "1.0" encoding = "UTF-8"?>

<web-app version = "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/javaee http://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, create struts.xml
<! DOCTYPE struts PUBLIC
"- / / Apache Software Foundation / / DTD Struts Configuration 2.0 / / EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="ajaxdemo" extends="struts-default">
<action name="UserListingAction">
<result> / userlisting.jsp </ result>
</ action>
<action name="UserDetailAction">
<result> / userdetail.jsp </ result>
</ action>
</ package>
</ struts>

III, page: userlisting.jsp
Displays list of users
<% @ Taglib prefix = "s" uri = "/ struts-tags"%>
<html>
<head>
<s:head theme="ajax"/>

</ head>
<script>
function show_user_details (id) (
document.frm_user.userid.value = id;
dojo.event.topic.publish ( "show_detail");
)
</ script>
<body>
<s:form name="frm_user">
<h1> User Listing </ h1>
<s:if test="userList.size> 0 ">
<table border="1">
<s:iterator value="userList">
<tr>
<td>
<s:a href="#" onclick="javascript:show_user_details('%{id}');return false;"> <s:property value="id" /> </ s: a>
</ td>
<td>
<s:property value="name" />
</ td>
</ tr>
</ s: iterator>
</ table>
</ s: if>
<s:hidden name="userid"/>
<s:url action="UserDetailAction" />
<s:div href="%{d_url}" theme="ajax" listenTopics="show_detail" formId="frm_user">
</ s: div>
</ s: form>
</ body>
</ html>

IV, page: userdetail.jsp, used to display user details from userlisting.jsp load
<% @ Taglib prefix = "s" uri = "/ struts-tags"%>
<h1> User Details </ h1>
<s:if test="userDetails != null">
<table>
<tr> <td> Id: </ td> <td> <s:property value="userDetails.id" /> </ td> </ tr>
<tr> <td> Name: </ td> <td> <s:property value="userDetails.name" /> </ td> </ tr>
<tr> <td> Email: </ td> <td> <s:property value="userDetails.email" /> </ td> </ tr>
<tr> <td> Address: </ td> <td> <s:property value="userDetails.address" /> </ td> </ tr>
</ table>
</ s: if>

5, ajaxdemo.action.UserListingAction.java, to generate a list of data users by userlisting.jsp show that in practical applications, this part of the data are generally obtained from the database.
package ajaxdemo.action;

import ajaxdemo.dto.UserListDTO;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;

/ ** *//** Populates the user listing data * /
public class UserListingAction extends ActionSupport (

private List <UserListDTO> userList; / / this is available in view automatically!
public String execute () throws Exception (

/ / Create 2 user objects and add to a list
setUserList ((List <UserListDTO>) new ArrayList ());
UserListDTO user = new UserListDTO ();
user.setId ( "gjose");
user.setName ( "Grace Joseph");
getUserList (). add (user);

user = new UserListDTO ();
user.setId ( "peter");
user.setName ( "PeterSmith");
getUserList (). add (user);
return SUCCESS;
)

public List <UserListDTO> getUserList () (
return userList;
)

public void setUserList (List <UserListDTO> userList) (
this.userList = userList;
)
)

6, ajaxdemo.action.UserDetailAction.java, when the userid is selected, the user access to detailed information through the dojo to call.
package ajaxdemo.action;

import ajaxdemo.dto.UserDetailDTO;
import com.opensymphony.xwork2.ActionSupport;

/**//* Populates user details for a user id selected * /
public class UserDetailAction extends ActionSupport (

private String userid;
private UserDetailDTO userDetails;

public String execute () throws Exception (
/ / Populate only when userid is selected
if (userid! = null & &! userid.equals (""))
populateDetail (userid);
return SUCCESS;
)

private void populateDetail (String id) (
userDetails = new UserDetailDTO ();
userDetails.setId (id);
userDetails.setName ( "The Complete Name");
userDetails.setEmail ( "admin@struts2.org");
userDetails.setAddress ( "rich street, lavish road, Struts Land");
)

public String getUserid () (
return userid;
)

public void setUserid (String userid) (
this.userid = userid;
)

public UserDetailDTO getUserDetails () (
return userDetails;
)

public void setUserDetails (UserDetailDTO userDetails) (
this.userDetails = userDetails;
)

)

7, ajaxdemo.action.UserDetailDTO.java, POJO, user information for the package
package ajaxdemo.dto;

public class UserDetailDTO (

private String id;
private String name;
private String email;
private String address;

public String getId () (
return id;
)

public void setId (String id) (
this.id = id;
)

public String getName () (
return name;
)

public void setName (String name) (
this.name = name;
)

public String getEmail () (
return email;
)

public void setEmail (String email) (
this.email = email;
)

public String getAddress () (
return address;
)

public void setAddress (String address) (
this.address = address;
)
)
OK, deployed after the input to test http://localhost:8080/ajaxdemo/UserListingAction.action.
When the list of userid to be after the point, javaScript notice Div tag from the URL to return to the contents of dynamic loading. This example, UserDetailAction made and passed to the user's information userdetail.jsp. userdetail.jsp to generate the final display of results, showing in the Div.
  相关解决方案