Configuration Action Struts2 learning

introduction

Action and how to make a corresponding request to match it?

Package and the package name space

       Action Struts2 used to organize the package, therefore, defined in the package definition Action, define Action by using <package ... /> <action ... ./> child element under to complete, and each element of the configuration package of a package.

example
 <package name="lee" extends="struts-default">
    </package>

Property description:

  • name :: required attributes. This attribute specifies the name of the package, the name of the package is being referenced by other packages key.
  • extends: optional. This attribute specifies the package to inherit other packages. Inherit other packages can be inherited Action defined in other packages, the interceptor definitions.
  • namespace: optional. This attribute defines the name space of the package.
  • abstract :: optional attributes. It specifies whether the packet is an abstract package. Abstract package can not contain Action to set.
    Function namespaces:
           In order to deal with the case of a Web application contains the same name of the Action. Struts2 way to manage namespace Action, the same namespace Action can not have the same name, different namespace Action may have the same name.
           Struts2 Action not supported as a separate namespace provided, but by a namespace attribute for the packet to the packet below the specified Struts Action have common namespace. If the attribute is not specified namespace configuration <package ... ./>, all the package under Action space is at a default packet.
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>

    <constant name="struts.custom.i18n.resources" value="mess"/>
    <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
    <constant name="struts.devMode" value="true"/>
    <package name="lee" extends="struts-default">
        <action name="login" class="LoginAction">
            <result name="error">error.jsp</result>
            <result name="success">welocme.jsp</result>
        </action>
    </package>
    <package name="Book" extends="struts-default" namespace="/book">
        <action name="loginBook" class="LoginAction">
            <result name="error">error.jsp</result>
            <result name="success">welocme.jsp</result>
        </action>
    </package>
</struts>

       As shown in the above code, wherein the packet does not specify a namespace lee, or the default of the package specified namespace Book Book;
       the URL when a package specifies a namespace, change all the packets should be treated Action namespace + Action name.

The basic configuration of Action

Action Definition

 <action name="login" class="LoginAction">
            <result name="error">error.jsp</result>
            <result name="success">welocme.jsp</result>
        </action>

Action name is the Struts2 URL processing it (if the request comprises a URL. Action suffix, should be removed. Action then suffix match) wherein Action class attribute specifies the implementation class.

Dynamic method using Action

       Struts2 dynamic method invocation refers to the form element acti on is not directly equal to the name of a Action, but in the following form to specify the form's action attribute.

<S:form action="login!execute">

Specify the method attributes and methods of use

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<package name="lee" extends="struts-default">
		<!-- 配置login Action,处理类为LoginRegistAction
			默认使用execute方法处理请求-->
		<action name="login" class="org.crazyit.app.action.LoginRegistAction">
			<!-- 定义逻辑视图和物理视图之间的映射关系 -->
			<result name="error">/WEB-INF/content/error.jsp</result>
			<result>/WEB-INF/content/welcome.jsp</result>
		</action>
		<!-- 配置regist Action,处理类为LoginRegistAction
			指定使用regist方法处理请求-->
		<action name="regist" class="org.crazyit.app.action.LoginRegistAction"
			method="regist">
			<!-- 定义逻辑视图和物理视图之间的映射关系 -->
			<result name="error">/WEB-INF/content/error.jsp</result>
			<result>/WEB-INF/content/welcome.jsp</result>
		</action>
		<action name="*">
			<result>/WEB-INF/content/{1}.jsp</result>
		</action>
	</package>
</struts>

       The above two logic Action, their corresponding classes are processed LoginRegistAction. Although two login and regist Action class have the same processing, but the processing logic is specified by the processing logic eleven different method, wherein the processing logic Action corresponding to the default login name execute method, called the regist corresponding to the A ction processing logic regist specified method.

<script type="text/javascript">
function regist()
{
	// 获取页面的第一个表单
	targetForm = document.forms[0];
	// 动态修改表单的action属性
	targetForm.action = "regist";
}
</script>
// Action包含的注册控制逻辑
	public String regist() throws Exception
	{
		ActionContext.getContext().getSession()
			.put("user" , getUsername());
		addActionMessage("恭喜您," + getUsername() + ",您已经注册成功!");
		return SUCCESS;
	}

In order to solve the above code is redundant
in configuration <a ction ... ./> element, the pattern string allowed when specifying the name attribute (i.e. with "*" represents one or more symbols of any buildings), then it may be class, method, and attribute <result ... ./> child element in the form of {N} used to represent the N-th substring preceding the asterisk (*) is matched.

Published 89 original articles · won praise 27 · views 6052

Guess you like

Origin blog.csdn.net/qq_41827511/article/details/104959333