这次来写个在struts2中继承ActionSupport例子:
注意:struts2配置就不多写了,可以在我之前发的文章中了解。
ActionSupport类本身实现了Action接口,所以继承ActionSupport类就相当于实现了Action接口。除此之外,ActionSupport类还实现了其它几个接口,来为程序员提供更多使用的功能,这些接口和Struts2的一些其他特性相结合,可以实现基本的数据验证功能和国际化。
1:基本数据的验证
实现数据功能的验证,需要在Action类中覆盖实现validate方法。在validate方法内部,对请求传递过来的数据进行校验,代码如下:
- 首先要在src下创建一个class(HelloWorldAction)。图下:
这里需要注意的是红色方框里的要写成 com.opensymphony.xwork2.ActionSupport;
- 打开HelloWorldAction.java,完成功能数据的验证,图下:
package com.hngy.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
private String account;
private String password;
private String submitFlag;
public String execute() throws Exception {
this.businessExecute();
return "toWelcome";
}
public void validate(){
if(account==null || account.trim().length()==0){
this.addFieldError("account", "账号不可以为空");
}
if(password==null || password.trim().length()==0){
this.addFieldError("password", "密码不可以为空");
}
if(password!=null && !"".equals(password.trim()) && password.trim().length()<6){
this.addFieldError("password", "密码长度至少为6位");
}
}
/**
* 示例方法,表示可以执行业务逻辑处理的方法,
*/
public void businessExecute(){
System.out.println("用户输入的参数为==="+"account="+account+",password="+password+",submitFlag="+submitFlag);
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSubmitFlag() {
return submitFlag;
}
public void setSubmitFlag(String submitFlag) {
this.submitFlag = submitFlag;
}
}
在validate方法中,可以对用户请求中传递过来的数据进行验证,同一个数据可以进行多方面的验证。
如果验证结果是数据不正确,那么就使用父类提供的addFieldError方法来添加验证的错误消息
2.配置struts.xml文件
在src下创建一个struts.xml文件,图下:
然后打开这个文件,编写代码:
<?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.devMode" value="true" /> <!-- 设置了程序的运行模式 -->
<constant name="struts.locale" value="zh_CN"/> <!-- 设置程序运行所使用的locale -->
<constant name="struts.i18n.encoding" value="utf-8"/> <!-- 设置程序运行时用的编码方式 -->
<!-- 正确设置后面两个参数,就可以解决Struts2的中文问题了。 -->
<package name="helloworld" extends="struts-default">
<action name="helloworldAction" class="com.hngy.action.HelloWorldAction">
<result name="toWelcome">/welcome.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
3.当输入信息错误时,将错误信息显示在前台页面上,代码如下:
- 这里在新建一个页面(welcome.jsp)来完成登录成功
2.编写login.jsp页面,图下:
代码块:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; utf-8">
<title>登录页面</title>
<style type="text/css">
ul,li {
list-style-type:none;
margin:0px;
float:left;
}
</style>
</head>
<body>
<form action="helloworldAction" method="post">
<input type="hidden" name="submitFlag" value="login"/>
<div>
<font color=red><s:fielderror fieldName="account"/></font>
<br/>
账号:<input type="text" name="account">
</div>
<div>
<font color=red><s:fielderror fieldName="password"/></font>
<br/>
密码:<input type="password" name="password">
</div>
<input type="submit" value="提交">
</form>
</body>
</html>
在jsp中利用<s:fielderror/>标签在相应的字段处输出错误信息。
注意:在struts.xml中的Action配置里面,添加一个名称为input的result配置,如果validate方法中,如果没有通过验证,就会自动跳转回到该action中名称为input的result所配置的页面。
总结:
validate方法会先于execute方法被执行,只有validate方法执行后,没有错误验证时,就会运行execute方法,否则会自动跳转到你所配置的input所对应的页面。
效果如下:
- 当账号和密码都为空时:
- 当账户和密码都输入:
注意在控制台上显示:
以上就是在[struts2]中继承ActionSupport的过程;还有些不懂得地方大家可以一起讨论讨论!
联系方式QQ:3064844881