转自:http://blog.csdn.net/hanxuemin12345/article/details/38763057

    项目中我们经常遇到这样的需求——页面部分刷新,例如:添加用户,转到添加用户页面时,页面自动加载了所有部门。

完整流程:选择所属部门,填写用户名和密码,点击“注册”,填写用户名后,需要立即检查数据库是否存在此用户名,如何在不刷新页面的情况下实现此效果?

Struts学习之自定义结果集

但看上面提出的问题并不难,情况很常见,都知道使用ajax实现,但是如何在struts中自定义结果集来实现ajax——这是这篇博客的重点。

转发、重定向、action2action都会使页面刷新,满足不了页面无刷新的需求,因此,可以自己定义一个结果集来解决(通过此结果集把服务器端(action)的数据回调到客户端)

一,步骤:

1,创建一个名称为Struts+AjaxResultWeb项目

2,导入Struts相关包

Struts学习之自定义结果集

3,配置web.xml文件

4,创建PoJo类——User.java

5,创建Action基类和子Action类——BaseActionUserAction.java

6,创建自定义结果集——AjaxResult.java

7,配置Struts.xml文件

8,创建页面——add.jsp

9,创建js文件——user_add.js

(注:还需引入jquery.js文件)

----web.xml:配置文件(注:配置Struts2的核芯过滤器)

 1 <span style="font-family:KaiTi_GB2312;font-size:18px;"><?xmlversionxmlversion="1.0" encoding="UTF-8"?>  
 2 <web-appversionweb-appversion="2.5"  
 3 xmlns="http://java.sun.com/xml/ns/javaee"  
 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
 7    
 8 <!-- 配置Struts2的核心的过滤器 -->  
 9 <filter>  
10 <filter-name>struts2</filter-name>  
11 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
12 </filter>  
13 <filter-mapping>  
14 <filter-name>struts2</filter-name>  
15 <url-pattern>/*</url-pattern>  
16 </filter-mapping>  
17    
18    
19   <welcome-file-list>  
20    <welcome-file>index.jsp</welcome-file>  
21   </welcome-file-list>  
22 </web-app></span>
View Code

相关文章:

  • 2021-11-17
  • 2022-02-07
  • 2022-12-23
  • 2021-08-07
  • 2021-05-28
  • 2022-12-23
  • 2021-10-05
  • 2021-11-22
猜你喜欢
  • 2021-09-08
  • 2021-10-07
  • 2021-05-20
  • 2022-12-23
  • 2021-11-09
  • 2021-11-18
  • 2022-12-23
相关资源
相似解决方案