Struts2 and AJAX - Using Dojo Div

本文介绍如何使用Struts2的div标签实现Ajax功能,通过一个简单的用户列表示例展示了Struts2与Dojo库结合进行Ajax操作的过程。
转自[url]http://www.struts2.org/struts2-and-ajax-using-dojo-div-part-i[/url]


Important : This example uses Struts 2.0.11. Struts2 Ajax support is experimental is undergoing rapid changes. I will update this example once Struts 2.1 is released.

One of the biggest improvements in Struts2 compared to Struts is its first class AJAX support. All the core Struts2 components are developed with a view of deploying in a highly responsive AJAX mode. In this post, I will look at the Struts2 div tag which can be used to render Ajax content. This component internally uses Dojo library for Ajax functionality.

To demonstrate this, I will create a very simple use case. The use case consists of a single screen which lists a number of users in a table. When a userid link is clicked, user details is shown at the bottom. For displaying user details we will use Ajax.

Following are the files used in this sample Struts2 project. The code listing is also given below.

1. web.xml
J2EE configuration file


<?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>

<?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>

2. struts.xml
Mvc configuration for struts


<!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" class="ajaxdemo.action.UserListingAction">
<result>/userlisting.jsp</result>
</action>
<action name="UserDetailAction" class="ajaxdemo.action.UserDetailAction">
<result>/userdetail.jsp</result>
</action>
</package>
</struts>


<!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" class="ajaxdemo.action.UserListingAction">
<result>/userlisting.jsp</result>
</action>
<action name="UserDetailAction" class="ajaxdemo.action.UserDetailAction">
<result>/userdetail.jsp</result>
</action>
</package>
</struts>

3. 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 id="frm_user" 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 id="d_url" action="UserDetailAction" />
<s:div id="user_details" href="%{d_url}" theme="ajax" listenTopics="show_detail" formId="frm_user" >
</s:div>
</s:form>
</body>
</html>



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 id="frm_user" 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 id="d_url" action="UserDetailAction" />
<s:div id="user_details" href="%{d_url}" theme="ajax" listenTopics="show_detail" formId="frm_user" >
</s:div>
</s:form>
</body>
</html>

4. userdetail.jsp
This displays user details and is loaded using Ajax from userlisting.jsp


<%@ 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>



<%@ 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. UserListingAction.java (under package ajaxdemo/action)
Prepares data for user listing and dispatches to userlisting.jsp. In a real application, this will connect to datasource through a business layer.


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;
}
}



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. UserDetailAction.java (under package ajaxdemo/action)
This loads the data when a userid is selected in userlisting.jsp. This is called via Dojo Ajax.


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;
}

}



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. UserListDTO.java (under package ajaxdemo/dto
This encapsulates details of a single user displayed in the list. Contains id and name. DTO stands for Data Transfer Object.

package ajaxdemo.dto;  
public class UserListDTO {
private String id;
private String name;

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;
}

}


package ajaxdemo.dto;  
public class UserListDTO {
private String id;
private String name;

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;
}

}

8. UserDetailDTO.java (unser package ajaxdemo/dto)
This encapsulates the complete details of a specific user.


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;
}
}


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;
}
}

When you access the URL http://localhost:8084/ajaxdemo/UserListingAction.action, UserListingAction prepares the data and it forwards to the userlisting.jsp. For Ajax, we use div tag in Struts2 tag library. Whenever a userid is clicked in the list, JavaScript function notifies the div tag to dynamically load content from a URL. In this case it is UserDetailAction which in turn gets data corresponding to the selected userid and dispatches to userdetail.jsp. The content returned by userdetail.jsp is loaded under Struts div tag.

As you can see the entire Ajax plumbing is hidden from the application developer and it is possible to quickly build highly responsive Web applications using this simple technique.

This sample application is hosted here. Check out to see the beauty of Struts2 Ajax. You can also download the entire source as a war file.


Note: We don’t want the user details to be loaded initially. But as of Struts 2.0.11, it is not possible to prevent the initial Ajax call. It seems that Struts 2.1 has introduced an attribute “preload” for preventing initial Ajax load. The work around here is to check the userid before returning any content.

In the next part we will look at each of the files in detail.
源码地址: https://pan.quark.cn/s/d1f41682e390 miyoubiAuto 米游社每日米游币自动化Python脚本(务必使用Python3) 8更新:更换cookie的获取地址 注意:禁止在B站、贴吧、或各大论坛大肆传播! 作者已退游,项目不维护了。 如果有能力的可以pr修复。 小引一波 推荐关注几个非常可爱有趣的女孩! 欢迎B站搜索: @嘉然今天吃什么 @向晚大魔王 @乃琳Queen @贝拉kira 第三方库 食用方法 下载源码 在Global.py中设置米游社Cookie 运行myb.py 本地第一次运行时会自动生产一个文件储存cookie,请勿删除 当前仅支持单个账号! 获取Cookie方法 浏览器无痕模式打开 http://user.mihoyo.com/ ,登录账号 按,打开,找到并点击 按刷新页面,按下图复制 Cookie: How to get mys cookie 当触发时,可尝试按关闭,然后再次刷新页面,最后复制 Cookie。 也可以使用另一种方法: 复制代码 浏览器无痕模式打开 http://user.mihoyo.com/ ,登录账号 按,打开,找到并点击 控制台粘贴代码并运行,获得类似的输出信息 部分即为所需复制的 Cookie,点击确定复制 部署方法--腾讯云函数版(推荐! ) 下载项目源码和压缩包 进入项目文件夹打开命令行执行以下命令 xxxxxxx为通过上面方式或取得米游社cookie 一定要用双引号包裹!! 例如: png 复制返回内容(包括括号) 例如: QQ截图20210505031552.png 登录腾讯云函数官网 选择函数服务-新建-自定义创建 函数名称随意-地区随意-运行环境Python3....
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值