struts2表单级验证,以最简单的登陆页面的用户名密码为例。
(1)新建login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>登陆(采用struts2标签)</title>
</head>
<body>
<s:form action="login" method="post">
<s:textfield name="userName" label="userName" />
<s:textfield name="password" label="password" />
<s:submit label="submit"/>
</s:form>
</body>
</html>
注:采用struts2标签,在页面头部加入<%@ taglib uri="/struts-tags" prefix="s"%>
(2)loginAction中
package login;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private String userName = "";
private String password = "";
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
/**
* 主方法入口
*/
public String execute() throws Exception{
if(this.userName==null || !"wu".equals(this.userName) || this.password==null || !"1".equals(this.password)){
this.addFieldError("userName", "userName or password error");
return "failure";
}
return "success";
}
/**
* 表单级验证
*/
public void validate(){
if(this.userName==null || "".equals(this.userName)){
this.addFieldError("userName", "userName is required");
}
if(this.password==null || "".equals(this.password)){
this.addFieldError("password", "password is required");
}
}
}
该类继承了ActionSupport父类。复写其中的validate()方法。
(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="login" extends="struts-default">
<action name="login" class="login.LoginAction">
<result name="success" >/loginSuccess.jsp</result>
<result name="input" >/login.jsp</result>
<result name="failure" >/login.jsp</result>
</action>
</package>
</struts>
分享到:
相关推荐
浅谈 Struts2 表单验证
struts2官方例子7-表单验证 xml形式的验证,主要是在相应的action类旁,新建一个对应的xml文件,进行验证
关于struts表单验证,关于struts表单验证,关于struts表单验证,
struts2 表单验证 注册 数据库操作
struts2表单验证
Struts2表单验证后回显错误信息 标签使用 李顺利Struts2表单验证后的错误信息显示格式-样式大全
在Eclipse中配置Struts2...Struts2―表单验证validate(html)对validate的type属性校验类型以及param 做了详细的解释说明。 struts2和struts的比较 让你更清楚的知道struts2和struts的不同之处。 struts教程(html)
NULL 博文链接:https://zhengbocong.iteye.com/blog/1961354
主要讲解了在Struts中流行的运用Validate技术对表单的各个字段进行动态的验证。附带有项目。
博文链接:https://nassir.iteye.com/blog/1601195
整合了struts2 jquery的formValidator表单验证的页面代码, 包含验证用户是否存在,提交表单等
Struts1的validator验证框架
详细说明struts如何进行表单验证
NULL 博文链接:https://famang.iteye.com/blog/296541
在Struts2中验证真的比较容易,可以在Action中直接写validate,也可以使用validate框架进行验证,这个我觉得比Struts1.x方便了很多,但是有个问题一直困扰了我,直到现在有个小程序要用到这个方面的知识,那就是...
Struts1.2动态表单验证
Struts2 极速表单验证框架 包含说明书,官方实例
myeclipse 6.5 + struts2 +ajax 实现表单注册验证源码
struts7 制作一个表单,对表单中的内容进行自定义验证。 struts8 全局性的验证xwork.default.invalid.fieldvalue={0} error struts9 进行局部性的验证,使用struts2标签库对register.jsp进行改进 strrts10 第七讲...
struts2 中,在JSP页面里,写入JS用来验证客户端提交的信息,还有一些正则表达式的用法。