SSH-Struts第一弹:ActionSupport类

时间:2021-02-20 19:37:21

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

  package com.candy.login;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {}

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

  addActionError(""); // Action错误提示
addFieldError("", ""); // 字段错误提示
addActionMessage(""); // 错误消息

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

 public static final java.lang.String SUCCESS = "success";
public static final java.lang.String NONE = "none";
public static final java.lang.String ERROR = "error";
public static final java.lang.String INPUT = "input";
public static final java.lang.String LOGIN = "login";

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

web.xml:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

struts.xml:

 <?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">
<!-- <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache
Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> -->
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<package name="struts2" extends="struts-default" namespace="/">
<global-results>
<result name="errorPath" type="redirect">
/errorPage.jsp
</result>
</global-results>
<action name="LoginAction" class="com.candy.login.LoginAction">
<result name="error">index.jsp</result>
<result>/WEB-INF/main.jsp</result>
</action>
</package> <!-- 引用其他模块的Struts.xml配置文件 -->
<!-- <include file="testStruts.xml"/> --> <!-- Apache Struts2官方网站 -->
<!-- http://struts.apache.org/release/2.0.x/ -->
</struts>