struts2實現ajax的兩種方式

來源:互聯網
上載者:User

標籤:

 

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="<%=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="styles.css">    -->    <script type="text/javascript" src="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="false" />    <constant name="struts.devMode" value="true" />    <constant name="struts.i18n.encoding" value="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的兩種方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.