0

Hello I´m newbie in Struts2, Ajax i´m trying make a hello world in Struts2+ajax when i push the button in the same page but i see the hello world in a action, Where is the problem? Sorry i´m learning english, thx.

index.jsp

<?xml version="1.0" encoding="UTF-8"?>
<%@taglib uri="/struts-tags" prefix="s" %>
<%@page pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %>
<%@taglib uri="/struts-tags" prefix="s" %>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" >
    <head>
    <sj:head jqueryui="true"/>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>DEMO AJAX</title>


    <sj:head jqueryui="true"/>
  </head>
  <body>
  <script type="text/javascript">
    $(document).ready(function(){
            // Processing hello button
            $('.buttonhello').click(function() {
            $.ajax({
                type : "POST",
                url : '${pageContext.request.contextPath}/index.html',
                success : function(response) {
                    $('.result').html(response);
                }
                });
            });
    });
    </script>   





       <s:form action="ajaxTest" theme="xhtml">
       <s:submit value="submit" />
       </s:form>


  </body>
</html>

I know that this part is bad not know what to write

$('.buttonhello').click(function() {

struts.xml

 <action name="ajaxTest" class="com.atorresbr.actions.AjaxTest" method="hello">
            <result name="success" >index.jsp</result>
        </action>

AjaxTest.java (action)

package com.atorresbr.actions;

import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class AjaxTest extends ActionSupport  {

    private static final long serialVersionUID = 1L;
          public void hello(){
            try {
                HttpServletResponse response =
                        ServletActionContext.getResponse();
                        response.setContentType("text/plain;charset=utf-8");

                        PrintWriter out = response.getWriter();
                        out.println("Hello Ajax");
                        out.flush();

            } catch (Exception e){

            }
        }
}

I have read many tutorials but this use dojo and I have read that is an outdated version. Thankyou very much.

ProSyth
  • 167
  • 1
  • 5
  • 17
  • You are doing *several* strange things, like returning a whole new page, the same you are in, from an AJAX request, using EL instead of to mount an action URL, calling a .html instead of an action from AJAX etc... When you perform an AJAX call, you make a call that won't change your current page. Once the result is returned, no matter what it is (JSON, JSP snippet (a "piece" of JSP), Stream etc...) you can use it to manipulate the page. Take a look at this full working example of the JSP snippet case: http://stackoverflow.com/a/24181930/1654265 – Andrea Ligios Mar 05 '15 at 09:23
  • Take a look at this too: http://stackoverflow.com/a/28276510/1654265 – Andrea Ligios Mar 05 '15 at 09:29
  • You are on the good way, but I can't see *hello world in action*. – Roman C Mar 05 '15 at 11:21

0 Answers0