Action development, wildcard, path problems, and constant usage in struts

Source: Internet
Author: User

1. Action Development

Several ways to develop

(1), inherited from the Actionsupport, (if using struts data validation work, you must use this function, because actionsupport to achieve data validation interface)

public class Useraction extends actionsupport{}

(2), implement the action interface, the content of the interface is as follows. (There are five constants and one method)

Pubic interface action{public   static final String success= "Success";   public static final String none= "NONE";   public static final String input= "INPUT";   public static final String login= "LOGIN";   Public Strng Execute () throws Exception;       }

(3), do not implement any classes, and do not inherit any interfaces.

public class useraction3{//action Business processing method public String login () {System.out.println ("Useraction Log (3)!"); Return "Success";}}

2. Access wildcard characters

The configuration information in struts can be optimized with * represented with {1}

At first, it was written like this.

<struts>    <package name= "config" namespace= "/" extends= "Struts-default" >    <action name= "Login "Class=" com.gqx.a_test. Useraction "method=" Login ">    <result name=" Success ">/index.jsp</result>    </action>    <action name= "register" class= "Com.gqx.a_test. Useraction "method=" register ">    <result name=" Success ">/index.jsp</result>    </action>      </package>  </struts>

You can now use wildcard characters to optimize

<!--use wildcards to optimize    --<action name= "user_*" class= "com.gqx.a_test. Useraction "method=" {1} ">    <result name=" Success ">/index.jsp</result>    </action>

Results such as


Now when the result of returning a string in action is not the same, you can write this

public class Useraction extends Actionsupport  {//action Business process method public String login () {System.out.println (" Useraction Log ()! "); return "Login";} Public String Register () {System.out.println ("useraction register ()!"); return "register";}}

At the same time the JSP page returned is different, this time can be configured Struct.xml

<action name= "user_*" class= "com.gqx.a_test. Useraction "method=" {1} ">    <result name=" {1} ">/{1}.jsp</result>    </action>

Results

3. The path matching principle in struts

General default Namespace= "/" When you change to

<package name= "config" namespace= "/user" extends= "Struts-default" >    

You will need to access this path http://localhost:8080/Struts_Study/user/user_register

At this time you can also access the http://localhost:8080/Struts_Study/user/a/b/user_register, the result is normal

But that's not going to http://localhost:8080/Struts_Study/a/b/user/user_register.

The reason is because when the user enters Http://localhost:8080/Struts_Study/user/a/b/user_register in the Address bar, enter the

On the server Tomcat side

LocalHost find access to that machine

8080 depending on the port number, find Tomcat

Struts_study Find the project name

/user/a/b to find out if there is this namespace, not found, continue down (find it back)

/user/a to find out if there is this namespace, not found, continue down (find it back)

/user to find out if there is this namespace, not found, continue down (find it back)

/default namespace, not found, error

  

Constant

Default access suffix in struts

Do in struts1, action in Struts2 (below)

So how to modify the suffix of the default access

The default suffix for STRUTS2 has the following code in Struts-core-2.3.4-1/org.apache.struts/default.properties

Struts.action.extension=action,,

Change in struts with constants

<!--global configuration--><!--Modify the Struts default access suffix--><constant name= "struts.action.extension" value= "Action,do," > </constant>

Specifies that the access suffix is do,action or none can

When the value= "action,do" access suffix can only be action or do

When the value= "action" access suffix is only action

Action development, wildcard, path problems, and constant usage in struts

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.