struts2.1中使用Ajax主题

1.将struts2-dojo-plugin-2.1.8.1.jar加入lib中 
2.在需要使用标签的jsp文件里写上<%@taglib prefix="sx" uri="/struts-dojo-tags" %> 
3.在<head>标签里写上<sx:head/> 
之后我们就可以开始使用struts2的ajax标签了

需注意的事项 
1、需引入struts2-dojo-plugin-2.1.8.1.jar 
2、页面标签的变化如下: 
ajax.jsp 
Java代码
  • <%@ page language="java" pageEncoding="UTF-8"%>   
  • <%@ taglib prefix="s" uri="/struts-tags"%>   
  • <%@ taglib prefix="sx" uri="/struts-dojo-tags"%><!-- 注意 -->   
  •   
  • <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
  • <html>   
  •    <head>         
  •      <title>Ajax Demo</title>   
  •      <sx:head/><!-- 注意 -->   
  •    </head>   
  •      
  •    <body>&nbsp;   
  • <h1>Ajax Dmeo:</h1>   
  • <div id="show">一个div</div>   
  • <s:form namespace="/demo" action="ajax!testAjax" method="post"><!-- 注意 -->   
  •      <s:textfield name="name"></s:textfield>   
  •      <sx:submit value="修改上面div的值" targets="show"></sx:submit><!-- 注意 -->   
  • </s:form>   
  •    </body>   
  • </html>  
    <%@ page language="java" pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <%@ taglib prefix="sx" uri="/struts-dojo-tags"%><!-- 注意 --> 
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>      
    <title>Ajax Demo</title>
    <sx:head/><!-- 注意 -->
    </head>
    <body>&nbsp; 
    <h1>Ajax Dmeo:</h1>
    <div id="show">一个div</div>
    <s:form namespace="/demo" action="ajax!testAjax" method="post"><!-- 注意 -->
    <s:textfield name="name"></s:textfield>
    <sx:submit value="修改上面div的值" targets="show"></sx:submit><!-- 注意 -->
    </s:form>
    </body>
    </html>
    Java代码
  • package cn.ibeans.demo.web.action;   
  •   
  • import org.apache.log4j.Logger;   
  • import org.apache.struts2.convention.annotation.Namespace;   
  • import org.apache.struts2.convention.annotation.Result;   
  • import org.apache.struts2.convention.annotation.Results;   
  •   
  • import com.opensymphony.xwork2.ActionSupport;   
  •   
  • @Namespace("/demo")   
  • @Results({   
  •     @Result(name="ok",location="ajaxResult.jsp")   
  • })   
  • public class AjaxAction extends ActionSupport {   
  •     private static Logger log= Logger.getLogger(AjaxAction.class);   
  •     private String data;   
  •   
  •     public String getData() {   
  •         return "服务器的返回值:"+data;   
  •      }   
  •   
  •     public void setData(String data) {   
  •         this.data = data;   
  •      }   
  •        
  •     public String testAjax() throws Exception{   
  •          log.info("ajax 调用成功!");   
  •         return "ok";   
  •      }   
  •   
  •     @Override  
  •     public String execute() throws Exception {   
  •          log.info("ajax 调用成功!");   
  •         return super.SUCCESS;   
  •      }   
  •        
  •        
  • }  
    package cn.ibeans.demo.web.action;
    import org.apache.log4j.Logger;
    import org.apache.struts2.convention.annotation.Namespace;
    import org.apache.struts2.convention.annotation.Result;
    import org.apache.struts2.convention.annotation.Results;
    import com.opensymphony.xwork2.ActionSupport;
    @Namespace("/demo")
    @Results({
    @Result(name="ok",location="ajaxResult.jsp")
    })
    public class AjaxAction extends ActionSupport {
    private static Logger log= Logger.getLogger(AjaxAction.class);
    private String data;
    public String getData() {
    return "服务器的返回值:"+data;
    }
    public void setData(String data) {
    this.data = data;
    }
    public String testAjax() throws Exception{
    log.info("ajax 调用成功!");
    return "ok";
    }
    @Override
    public String execute() throws Exception {
    log.info("ajax 调用成功!");
    return super.SUCCESS;
    }
    }
    ajaxResult.jsp: 
    Java代码
  • <%@ taglib prefix="s" uri="/struts-tags" %>   
  •   
  • <%   
  •      request.setAttribute("decorator", "none");   
  •      response.setHeader("Cache-Control","no-cache"); //HTTP 1.1   
  •      response.setHeader("Pragma","no-cache"); //HTTP 1.0   
  •      response.setDateHeader ("Expires", 0); //prevents caching at the proxy server   
  •      System.out.println("ajaxResult.jsp is called");   
  • %>   
  •   
  • Result:   <s:property value="data"/><br/>  
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <%
    request.setAttribute("decorator", "none");
    response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
    response.setHeader("Pragma","no-cache"); //HTTP 1.0
    response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
    System.out.println("ajaxResult.jsp is called");
    %>
    Result:  <s:property value="data"/><br/>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值