首页 > 代码库 > struts2实现ajax的两种方式

struts2实现ajax的两种方式

 

jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href=http://www.mamicode.com/"<%=basePath%>">        <title>My JSP index.jsp starting page</title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href=http://www.mamicode.com/"styles.css">    -->    <script type="text/javascript" src=http://www.mamicode.com/"jquery/jquery1.8.3.js" ></script>        <script>        function testAjax(){            var username=$("#ajax_username").val();            $.post(            "Test_ajax2.action",            {username:username},            function(date){                    alert(date);                }            );        }    </script>  </head>    <body>    <input type="text" id="ajax_username"><button onclick="testAjax()" >提交</button>  </body></html>

 

struts.xml:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <constant name="struts.enable.DynamicMethodInvocation" value=http://www.mamicode.com/"false" />    <constant name="struts.devMode" value=http://www.mamicode.com/"true" />    <constant name="struts.i18n.encoding" value=http://www.mamicode.com/"utf-8"/>        <package name="example" namespace="/">        <action name="*_*" class="com.web.action.{1}Action" method="{2}">        </action>    </package>        <package name="example1" namespace="/" extends="json-default">        <action name="Test_ajax2" class="com.web.action.TestAction" method="ajax2">        </action>    </package></struts>

 

TestAction.java:

package com.web.action;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class TestAction extends ActionSupport {    private String username;    public void setUsername(String username) {        this.username = username;    }    public void ajax() throws IOException{        HttpServletResponse response=ServletActionContext.getResponse();        response.setContentType("text/html; charset=utf-8");        PrintWriter writer=response.getWriter();                //只有这样才可以获取到值,不写下面这行代码username的值为null        this.username=ServletActionContext.getRequest().getParameter("username");                writer.print("hello,"+username);        writer.flush();        writer.close();    }        public void ajax2() throws IOException{        System.out.println(1);        HttpServletResponse response=ServletActionContext.getResponse();        response.setContentType("text/html; charset=utf-8");        PrintWriter writer=response.getWriter();        writer.print("hello,"+username);        writer.flush();        writer.close();    }}

 

第一种方式使用的方法是我的方法ajax(),但是使用这种方式的时候:ajax和request耦合,使用Action无法自动为username属性注入值。我暂时不知道为什么,有知道原因的告诉下,谢谢!

 

第二种方式使用的是方法ajax2(),这种方式的的前提是使用插件:

也就是struts中需要导入jar:

技术分享

然后你的package必须继承json-default

这样你的Action就可以为ajax参数自动赋值了。

 

struts2实现ajax的两种方式