Struts Development & lt; detailed configuration of actions in struts. 2 & gt;, strutsaction

Source: Internet
Author: User

Struts development <detailed configuration of actions in struts. 2>, strutsaction
Create a project StrutsDemo1 in eclipse. [For struts configuration, see struts development. <configure struts in eclipse. 1>

The directory structure is as follows:



Method 1

Create UserAction

package fzl.user.struts.demo;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {public String list(){System.out.println("list");return "success";}public String input(){System.out.println("input");return "success";}public String add(){System.out.println("add");return "success";}}

Reconfigure in struts

<? 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> <package name = "default" namespace = "/" extends = "struts-default"> <! -- First, configure multiple actions --> <action name = "add" class = "fzl. user. struts. demo. userAction "method =" add "> <result name =" success ">/WEB-INF/User/add. jsp </result> </action> <action name = "input" class = "fzl. user. struts. demo. userAction "method =" input "> <result name =" success ">/WEB-INF/User/input. jsp </result> </action> <action name = "list" class = "fzl. user. struts. demo. userAction "method =" list "> <result name =" success ">/WEB-INF/User/list. jsp </result> </action> </package> </struts>

Create input. jsp, list. jsp, and add. jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


Run tomcat and enter http: // localhost: 9000/strustDemo1/input in the browser.


Similarly, input the response directory to get the corresponding page output.

Method 2

You can directly change UserAction and struts. xml without any changes.


package fzl.user.struts.demo;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {public String list(){System.out.println("list");return "list";}public String input(){System.out.println("input");return "input";}public String add(){System.out.println("add");return "add";}}


<action name="User" class="fzl.struts.demo.UserAction">     <result name="add">/WEB-INF/User/list.jsp</result>    <result name="input">/WEB-INF/User/input.jsp</result>    <result name="list">/WEB-INF/User/add.jsp</result>     </action>

Enter http: // localhost: 9000/StrutsDemo2/User! Add to get the corresponding page



Method 3


package fzl.user.struts.demo;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {public String list(){System.out.println("list");return "success";}public String input(){System.out.println("input");return "success";}public String add(){System.out.println("add");return "success";}}

If you are not familiar with the wildcard configuration

<?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><package name="default" namespace="/" extends="struts-default"> <action name="*_*" class="fzl.user.struts.demo.{1}Action"method="{2}"><result>/WEB-INF/{1}/{2}.jsp</result></action> </package></struts>


There are three methods to configure actions. The last two methods are usually used in development.



How to configure strutsxml for jump of two actions in struts2

In the action configuration, there is an Action type that is "redirectAction" (redirected to an action) and chain (called the action request chain ).
By the way, struts2's action type:
Chain is used to process the Action chain
Dispatcher is used to redirect to pages. Generally, JSP
Redirect redirects to a URL
RedirectAction (or redirect-action) redirects to an Action

Redirect: redirection. The data in the first Action cannot be displayed on the new page, because the underlying call is response. sendRedirect ("... ") method. data within the request range cannot be shared. The parameter and dispatcher usage are the same;
Chain: forward an action request with the original status to the new action. The two actions share an ActionContext. The actionName specifies the name of the new action to be switched, and the method specifies the method to be switched, namespace specifies the namespace of the new Action. If this parameter is left blank, it indicates that the new Action is in the same namespace as the original Action.
Redirect-action: redirects to another Action. The parameter is used in the same way as the chain parameter. You can specify a new name for the attribute in the original Action to be added to the new Action, you can add <param name = "B" >$ {a} </param> to the Result tag, which indicates that the value of variable a in the original Action is transferred to B, the next Action can be operated using B in the value stack. Note that if the value is Chinese, some encoding is required, because Tomcat does not support direct URL transfer of Chinese characters by default!

Example:
After saving the discussion comments under the discussion topic, you can directly jump to the page where all the discussions under the discussion topic are displayed.
<! -- Save the discussion comments -->
<Action name = "saveDiscussContext" class = "com. discuss. DiscussAction"
Method = "saveDiscussContext">
<Result name = "success" type = "chain"> <! -- Note that type is the chain type -->
<Param name = "namespace">/discuss </param>
<Param name = "actionName"> findDiscussContextBySeqNum </param>
<! -- ActionName is another configured action findDiscussContextBySeqNum. It is the action to find all the comments discussed -->
</Result>
</Action>
The landlord understands it. If you still don't know how to query online materials !! ·

How does struts2 configure actions?

In jsp, you must name the action in the form.
<Form action = "login. action" method = "post" id = "login">
...
</Form>
Create corresponding action class
Public class LoginAction {
...
Public String excute (){
Return SUCCESS;
// Return ERROR;
}
}
Configure in struts. xml
<Action name = "login" class = "com. web. action. member. LoginAction">
<Result type = "redirectAction"> afterLogin </result>
<Result name = "error">/login. jsp </result>
</Action>

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.