2014-02-19 103 views
0

我有以下的JSP如何通過使用AJAX

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
     pageEncoding="ISO-8859-1"%> 
    <%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> 
    <%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%> 
    <%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> 



    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
    <html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
    <title>Get the best deal on Qnatas Partner credit cards</title> 

<script type="text/javascript"> 

function doAjaxPost() { 
        // get the form values 
        var name = $(#name).val(); 

        $.ajax({ 
         type: "POST", 
         url: "/cardselector/mailAction.do", // this is my action call 
         data: "name=" + name, 
         success: function(response){ 
          // we have the response 
          $('#info').html(response); 
         }, 
         error: function(e){ 
          alert('Error: ' + e); 
         } 
        }); 
       } 
</script> 
    </head> 
    <body> 
    <p>Welcome to card selector</p> 
    <html:form action="/cardsList" target="cardsListForm"> 
     <html:text name="cardsListForm" property="message" maxlength="250"/> 
     <div id="name">sample</div> 
     <input type="text" id="name">myname</input> 

     <input type="button" value="Say Hello"><br/> 
    <div id="info" style="color: green;"></div> 
    </html:form> 

    </body> 
    </html> 

,這是我的形式

import java.util.List; 

import org.apache.struts.action.ActionForm; 

public class CardsListForm extends ActionForm{ 
    private static final long serialVersionUID = 1L; 

    private String message;  

    public String getMessage() { 
     return message; 
    } 

    public void setMessage(String message) { 
     this.message = message; 
    } 

} 

這是值傳遞(這不是一個形式值)在struts 1.3行動我的行動

public class MailAction extends Action { 

    private CardManagementService cardManagementService; 

    @Override 
    public ActionForward execute(ActionMapping mapping, ActionForm form, 
      HttpServletRequest request, HttpServletResponse response) 
      throws Exception { 
     try { 
       // Need to get the value of name from jsp input text 

     } catch (Exception e) { 
      System.out.println("Error:" + e.getMessage()); 
      e.printStackTrace(); 
     } 

     return mapping.findForward("success"); 
    } 


} 

現在我想用一個按鈕的點擊阿賈克斯從JSP TP我的動作類通過MYNAME的價值。請注意這個名字不是我的表單類的一個屬性。

請問有沒有可能的幫助!

+0

哪裏是在你的代碼AJAX? – Jai

+0

我已經添加了Ajax功能在我的代碼 – nallskarthi

回答

1

負載jquery,通過這個按鈕,這樣的點擊一個POST請求,將數據傳遞給你的行動,希望這有助於

HTML:

<input type="button" id="button" value="Say Hello"> 

JS:

$(document).ready(function(){ 
     $("#button").click(function(){ 

      var name = $('#name').val(); //your code has $(#name) which is not selecting required input element! 
     $.post("/cardselector/mailAction.do",{"name" : name},function(response) 
      { 
       alert("done"); 
       console.log(response); //see what response you are getting   
     }); 
     }) 

; });

+0

如果您發佈這樣如何讓動作類 – nallskarthi

+0

只要你現在正在做同樣的方式name的值?應該可以工作,只需訪問'POST'變量的'name'鍵,在成功時發送'response'來檢查! – Aditya

+0

嗨Aditya,已經嘗試我有警覺它完成:)。你可以請張貼代碼訪問「名」在動作類 – nallskarthi

0

使用jQuery $.post。例如:

$.post("youactionname",params); //this is just an example syntax. 

此外,

<input type="text" id="name">MYNAME</input>

你在哪裏定義MYNAME?

+0

感謝兄弟我正在嘗試 – nallskarthi

1

改變這一點:

var name = $(#name).val(); 

這樣:

var name = $('#name').val(); //<---you had missing quotes 

data: {name:name}, //<----send your user name as an object. 

,做一個內嵌關閉輸入elems的:

<input type="text" id="name" /> 

,我沒有找到任何點擊listner在你的代碼中,所以我建議你這樣做:

function doAjaxPost() { 
    // get the form values 
    var name = $('#name').val(); // <---missing quotes 

    $.ajax({ 
     type: "POST", 
     url: "/cardselector/mailAction.do", // this is my action call 
     data: {name:name}, //<----and send it like this 
     dataType: 'text', //<-----if response is string then use dataType:"text" 
     success: function(response){ 
       // we have the response 
       $('#info').html(response); 
     }, 
     error: function(e){ 
       alert('Error: ' + e); 
     } 
    }); 
} 

$(function(){ 
    $('[type="button"]').on('click', doAjaxPost); 
}); 
+0

謝謝我已添加onclick listner – nallskarthi

+0

仍然無法找到PLZ更新帖子。 – Jai