SSH-Struts第一弹:ActionSupport类

Action继承了com.opensymphony.xwork2.ActionSupport。

1  package com.candy.login;

2  import com.opensymphony.xwork2.ActionSupport;

3  public class LoginAction extends ActionSupport {}

继承了ActionSupport后,可以在execute()里加入以下验证信息:

 

1  addActionError(""); // Action错误提示

2  addFieldError("", ""); // 字段错误提示

3  addActionMessage(""); // 错误消息

就目前继承ActionSupport的作用有两个:
1、addActionError("")/addFieldError("","")/addActionMessage("")三个方法都是从这个类继承的,可以在自己的Action中使用。
2、我找到的ActionSupport的五个静态常量,也可以在自己的Action中的使用。同时,在struts.xml中<result>/main.jsp</result>和<result name="success">/main.jsp</result>的功能是一致的。

1 public static final java.lang.String SUCCESS = "success";

2 public static final java.lang.String NONE = "none";

3 public static final java.lang.String ERROR = "error";

4 public static final java.lang.String INPUT = "input";

5 public static final java.lang.String LOGIN = "login";

同时,发下web.xml和struts.xml

web.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>

 2 <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"

 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 4     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 5     <display-name>Struts</display-name>

 6     <filter>

 7         <filter-name>struts2</filter-name>

 8         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

 9     </filter>

10     <filter-mapping>

11         <filter-name>struts2</filter-name>

12         <url-pattern>/*</url-pattern>

13     </filter-mapping>

14     <welcome-file-list>

15         <welcome-file>index.jsp</welcome-file>

16     </welcome-file-list>

17 </web-app>

struts.xml:

 1 <?xml version="1.0" encoding="UTF-8" ?>

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

 3 <!-- <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache 

 4     Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> -->

 5 <struts>

 6     <constant name="struts.i18n.encoding" value="UTF-8"></constant>

 7     <package name="struts2" extends="struts-default" namespace="/">

 8         <global-results>

 9             <result name="errorPath" type="redirect">

10                 /errorPage.jsp

11             </result>

12         </global-results>

13         <action name="LoginAction" class="com.candy.login.LoginAction">

14             <result name="error">index.jsp</result>

15             <result>/WEB-INF/main.jsp</result>

16         </action>

17     </package>

18 

19     <!-- 引用其他模块的Struts.xml配置文件 -->

20     <!-- <include file="testStruts.xml"/> -->

21 

22     <!-- Apache Struts2官方网站 -->

23     <!-- http://struts.apache.org/release/2.0.x/ -->

24 </struts>

你可能感兴趣的:(struts)