Use Struts2 and jQuery EasyUI to implement a simple CRUD System (2) -- interaction between ajax and struts2

Source: Internet
Author: User

Use Struts2 and jQuery EasyUI to implement a simple CRUD System (2) -- interaction between ajax and struts2

When I wrote my blog, I was wondering if someone would ask me a question I asked someone else. After I met some people, I had some questions about technology and other issues, and I got some inspiration from experienced people. Then I wrote my own things and someone asked me, which is a wonderful thing. I have received help from others. When someone else asks for help, I also hope that I can tell others what I want.

 

Next to the previous article: Use Struts2 and jQuery EasyUI to implement a simple CRUD System (I)-from scratch, ajax interacts with Servlet, and then let ajax interact with Servlet. Next is the combination with struts2.

 

I don't know much about struts. I just can't make up for it.

 

The jar package required by struts.

 

Add the strut2 filter to web. xml.

 

 
 
  
   ZeroToOne
  
  
  
   
    struts2
   
   
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
   
  
  
   
    struts2
   
   
    /* 
   
  
  
   
    Hello
   
   
    Servlet.HelloServlet
   
  
  
   
    Hello
   
   
    /helloServlet
   
  
 

Add struts. xml to the root directory

 

 

 
 
  
   
    
     
      
       
        
         
          /error.jsp 
         
         
          /hello.jsp 
         
        
        
         
         
        
         /hello.jsp
        
       
      
      
      
     
    
   
  
 


Create an AjaxAction to implement the Struts2 Action interface. Output directly using PrintWriter as in the first article,
Return Success.

 

AjaxAction class

 

public class AjaxAction implements Action{public String ajax() throws Exception {System.out.println(action execute);ServletActionContext.getResponse().getWriter().println(struts data response to ajax);return SUCCESS;}@Overridepublic String execute() throws Exception {return null;}}

 

Because of the existence of the strutsfilter, when ajax.html clicks the ajax button, the ajaxaction is requested, the namespace is/, and the namespace is/
Package label.

According to the struts. xml configuration, find the tag named "ajaxaction" in the action tag. The ajax method that requests action. AjaxAction.

After the execution, the result is success. The hello. jsp page is displayed.

 

Hello. jsp

 

<%@ page language=java contentType=text/html; charset=ISO-8859-1    pageEncoding=ISO-8859-1%>
The response do not response to ajax

 

 

During execution, the following message is displayed: java. lang. ClassNotFoundException: org. apache. struts2.dispatcher. ng. filter. StrutsPrepareAndExecuteFilter.
Solution: project-> properties-> Deployment Assembly-> add-> java build path entries. The build path is loaded into the class path. When tomcat is started, filter must go through struts,
The web-inf/lib does not have a jar package, so the error of non-class error is reported. Therefore, you need to manually copy the file to the lib directory or use the above method.
 

Then we can get the result like the interaction between ajax and Servlet, and get the string output by PrintWriter.

But the result does not get the string output by ServletActionContext. getResponse (). getWriter (). println (struts data response to ajax.

Xmlhttp. responseText obtains the content of hello. jsp.

 

Then we need to allow struts2 to access this method without redirecting to hello. jsp. The value Action. NONE is found.

In struts's official guides: https://struts.apache.org/docs/result-configuration.html

With its description, Returning ActionSupport. NONE (or null) from an Action class method causes the results processing to be skipped. this is useful if the action fully handles the result processing such as writing directly to the HttpServletResponse OutputStream.

 

Returning ActionSupport. NONE (or null) from the Action class can omit the processing of the result. It is useful when you want to completely control the processing of the result, as if you want to directly output the result through HttpServletResponse OutputStream.

Yes, that's how we use it.

Change the Return Statement of AjaxAction:

 

ServletActionContext.getResponse().getWriter().println(struts data response to ajax);return Action.NONE;

Remove the following in struts. xml:

 

 

 
  /hello.jsp
 

Finally, let struts interact with ajax.

 

 

Code

 

In addition to using none to return data, success can also be used normally. Of course, the results will jump to a specific page as mentioned above.

But in another way, it is also possible to put what we want to output on this page.

If you place the content in the Action class on the specified page, you can use ActionContext to place the specific content, and then extract the value of the specific content from the page using Request. getAttribute.

You can use out. write to output data.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.