Struts2 in action with out

Source: Internet
Author: User

In Java development, sometimes in the Struts2 action method only need to directly output some text information, such as Ajax request, return only need some text information, STRUTS2 also support, first write the action class, In the action class of the method to get the output stream and then the text output, and then configure in the Struts2 configuration file action,[later steps can not be used] if there is a result then set its Type property to plaintext, that is,

[HTML]View Plaincopy
    1. <result name= "xName" type= "plaintext"... </result>

At the same time in the result element to set some parameters to the action can be completed, the following two kinds of scenarios are as follows:

Scenario One: [Common, deprecated]

1, configure the action in the Struts configuration file
Easy to understand, first look at the struts2 configuration file (the actual configuration file should be completed in the final step), which only defines an action, note the result element of the various properties and the internal PARAM elements;

[HTML]View Plaincopy
  1. <? XML version= "1.0" encoding="UTF-8" ?>
  2. <! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.1//en"
  3. "Http://struts.apache.org/dtds/struts-2.1.dtd">
  4. <struts>
  5. <package name= "myStruts2" extends="Struts-default" namespace="/">
  6. <action name="testaction" class="com.xxxName.actions.TestAction" method=" Myexecute ">
  7. <result name= "success" type="plaintext">
  8. <!--The result of the Name= "success" must have--
  9. <param name="CharSet">utf-8</param>
  10. <!--set the output character encoding to prevent characters other than English and numerals (such as Chinese) from appearing garbled--
  11. <param name="Location">/testout.jsp</param>
  12. <!--set the dependent local file, the file must actually exist, and the path is correct, but the contents of the file can be empty, only local dependency function
  13. </result>
  14. </Action>
  15. </Package>
  16. </struts>


2. Create a local attachment file

Create a JSP file under the appropriate path, provide an action to attach, the contents of the file can be empty, can also contain HTML elements, etc., but actually do not have to, just provide action for local attachment, can be multiple actions to share the file;

3,action class file Writing

[Java]View Plaincopy
  1. Package com.xxxName.actions;
  2. Import java.io.IOException;
  3. Import Java.io.PrintWriter;
  4. Import Java.util.Date;
  5. Import javax.servlet.ServletException;
  6. Import Org.apache.struts2.ServletActionContext;
  7. Import Com.opensymphony.xwork2.ActionSupport;
  8. Public class Testaction extends actionsupport{
  9. private String ContentType = "Text/html;charset=utf-8";
  10. Public String Myexecute () throws Servletexception, ioexception{
  11. //Specify Output content type and encoding
  12. Servletactioncontext.getresponse (). setContentType (ContentType);
  13. //Get the output stream, and then use the
  14. PrintWriter out = Servletactioncontext.getresponse (). Getwriter ();
  15. try{
  16. //Output text information
  17. Out.print ("Hello Java, here is struts2, output text information directly in the action method.");
  18. Out.print ("Time:" + (new Date ()). GetTime ());
  19. Out.flush ();
  20. Out.close ();
  21. }catch (Exception ex) {
  22. Out.println (Ex.tostring ());
  23. }
  24. return "Success";
  25. Because the direct output, so the above line is actually useless, just in order to meet the function return value of the requirements, you can also return null, that is, return "";
  26. can also change the function return type to void type, can not return, direct output; Note: Although the return is OK, the configuration file still needs to name= the result element of "sccess"
  27. }
  28. }

Scenario two, [concise, recommended]

Scenario two is similar to the above principle, specific different reasons for analysis, because it is the direct output of text information, that is, the action method is not necessary to return operations, so the function return value type can be void, and then see the action is only in the configuration file Struts2 configuration, Just let Struts2 find the action, no longer need any result element, the whole process is: let Struts2 find the action, and then in the action method directly output text information, as follows:

1, configure the action in the Struts configuration file

Easy to understand, first look at the struts2 configuration file (the actual configuration file should be completed in the final step), in which only the action is configured in STRUTS2;

[HTML]View Plaincopy
  1. <! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.1//en"
  2. "Http://struts.apache.org/dtds/struts-2.1.dtd">
  3. <struts>
  4. <package name= "myStruts2" extends="Struts-default" namespace="/">
  5. <action name="test2action" class="com.xxxName.actions.Test2Action" method=" Myexecute ">
  6. <!--only configures the action in Struts2 to allow Struts2 to recognize/find the action without any result, as it returns void- --
  7. </Action>
  8. </Package>
  9. </struts>


2,action class file Writing

[Java]View Plaincopy
  1. Package com.xxxName.actions;
  2. Import java.io.IOException;
  3. Import Java.io.PrintWriter;
  4. Import Java.util.Date;
  5. Import javax.servlet.ServletException;
  6. Import Org.apache.struts2.ServletActionContext;
  7. Import Com.opensymphony.xwork2.ActionSupport;
  8. Public class Test2action extends actionsupport{
  9. private String ContentType = "Text/html;charset=utf-8";
  10. public void Myexecute () throws Servletexception, ioexception{ //return type void
  11. //Specify Output content type and encoding
  12. Servletactioncontext.getresponse (). setContentType (ContentType);
  13. //Get the output stream, and then use the
  14. PrintWriter out = Servletactioncontext.getresponse (). Getwriter ();
  15. try{
  16. //Direct text Manipulation
  17. Out.print ("Hello Java, here is struts2, output text information directly in the action method.");
  18. Out.print ("Time:" + (new Date ()). GetTime ());
  19. Out.flush ();
  20. Out.close ();
  21. }catch (Exception ex) {
  22. Out.println (Ex.tostring ());
  23. }
  24. //Description: Because the function return type is void type, can not return, direct output;
  25. }
  26. }

Scenario two eliminates the establishment of local files, and the configuration file has become quite simple, only need to get the output stream in the action method, set the output type and encoding, and then directly to the text output operation, the recommended use of scenario two;

Of course, this is only about the key steps involved in this title, the whole project needs to be set up to run, and finally, if the project is built and configured to the server (such as Tomcat),
Assuming that the Tomcat port is 8080 and the project name j2ee001,action matches the suffix. Action, then access the following URL:

Http:localhost:8080/j2ee001/test2action.action
You can see the direct output text instead of going to another page.

Struts2 in action with out

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.