在上篇文章中,已经写了Struts2环境配置,算是搭建了一个Struts2框架
那么今天,我接着昨天的往下说。
ActionSupport类
ActionSupport类本身实现了Action接口,所以继承ActionSupport类就相当于实现了Action接口。
在下面这个例子中我们用到了validate方法,addFieldError方法和<s:fielderror/>标签。
(1)validate方法:数据校验(使用详情看下面例子代码)
(2)addFieldError方法和<s:fielderror/>标签:验证错误信息和在相应的字段处输出错误信息。(格式如下)
addFieldError("字段名","错误信息")给一个字段(属性)添加错误消息
this.addFieldError("pwd", "错误信息1");
this.addFieldError("pwd", "错误信息2");
显示消息的方法1:标签是:<s:fielderror />显示全部的错误消息;
显示消息的方法2:
<s:fielderror>
<s:param>field1 </s:param> 显示指定的 field1字段的 错误消息
<s:param>field2 </s:param> 显示指定的 field2字段的 错误消息
</s:fielderror>
如何继承ActionSupport?
一个简单的例子:
效果图1:
效果图2:
效果图3:
话不多说,先上代码镇楼
1.login.jsp(新建视图层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>Insert title here</title>
<style type="text/css">
ul,li {
list-style-type:none;
margin:0px;
float:left;
}
</style>
</head>
<body>
<form action="test/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>
welcome.jsp(新建视图层jsp页面)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'welcome.jsp' starting page</title>
</head>
<body>
欢迎登陆!
</body>
</html>
2.HelloWorldAction.java(新建class文件,命名为HelloWorldAction.Java,继承ActionSupport)
package com.hnpi.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;
}
}
3.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>
<package name="default" namespace="/test" extends="struts-default">
<action name="register" class="com.hnpi.action.RegisterAction" method="action">
<result name="success">/index.jsp</result>
</action>
<action name="helloworldAction" class="com.hnpi.action.HelloWorldAction">
<result name="toWelcome">/welcome.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
4.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
</web-app>
是不是突然发现我的步骤少了一些??!没错,我是故意的,因为懒。。。不要吐槽我...QAQ......
代码写到这里就结束了,想运行的小伙伴也可以直接搜索我上传的资源。就是下面的地址,也可以直接关注我啦,我会时不时的更新哒!
https://download.csdn.net/download/weixin_41425059/10756672