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(""); // 错误消息
同时,发下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> 18
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 <struts> 4 <constant name="struts.i18n.encoding" value="UTF-8"></constant> 5 <package name="struts2" extends="struts-default" namespace="/"> 6 <global-results> 7 <result name="errorPath" type="redirect"> 8 /errorPage.jsp 9 </result> 10 </global-results> 11 <action name="LoginAction" class="com.candy.login.LoginAction"> 12 <result name="error">index.jsp</result> 13 <result>/WEB-INF/main.jsp</result> 14 </action> 15 </package> 16 17 <!-- 引用其他模块的Struts.xml配置文件 --> 18 <!-- <include file="testStruts.xml"/> --> 19 20 <!-- Apache Struts2官方网站 --> 21 <!-- http://struts.apache.org/release/2.0.x/ --> 22 </struts>
SSH-Struts第二弹:一个Form提交两个Action
SSH-Struts第一弹:ActionSupport类,布布扣,bubuko.com
原文:http://www.cnblogs.com/Candies/p/3604148.html