struts简单验证
来源:互联网 发布:win 7dock软件 编辑:程序博客网 时间:2024/05/17 06:14
功能:登录时提醒要输入用户名与密码
login.jsp
<%...@ page language="java" pageEncoding="ISO-8859-1"%>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-template" prefix="template" %>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-nested" prefix="nested" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
<head>
<html:base />
</head>
<body>
<html:errors/><hr>
<html:form action="/Login" method="POST">
username:<html:text property="username"/>
password:<html:password property="password"/>
<html:submit/>
</html:form>
</body>
</html:html>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-template" prefix="template" %>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-nested" prefix="nested" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
<head>
<html:base />
</head>
<body>
<html:errors/><hr>
<html:form action="/Login" method="POST">
username:<html:text property="username"/>
password:<html:password property="password"/>
<html:submit/>
</html:form>
</body>
</html:html>
index.jsp
<%...@ page language="java" pageEncoding="ISO-8859-1"%>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-template" prefix="template" %>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-nested" prefix="nested" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
<head>
<html:base />
<title>index.jsp</title>
</head>
<body>
<h1>success!</h1>
</body>
</html:html>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-template" prefix="template" %>
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-nested" prefix="nested" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
<head>
<html:base />
<title>index.jsp</title>
</head>
<body>
<h1>success!</h1>
</body>
</html:html>
LoginForm.java
package app;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class LoginForm extends ActionForm...{
private String username;
private String password;
public String getPassword() ...{
return password;
}
public void setPassword(String password) ...{
this.password = password;
}
public String getUsername() ...{
return username;
}
public void setUsername(String username) ...{
this.username = username;
}
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)...{
ActionErrors errors=new ActionErrors();
if(this.username==null||"".equals(username))...{
errors.add("username",new ActionError("username.null"));
}
if(this.password==null||"".equals(password))...{
errors.add("password",new ActionError("password.null"));
}
return errors;
}
public void reset()...{}
}
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class LoginForm extends ActionForm...{
private String username;
private String password;
public String getPassword() ...{
return password;
}
public void setPassword(String password) ...{
this.password = password;
}
public String getUsername() ...{
return username;
}
public void setUsername(String username) ...{
this.username = username;
}
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)...{
ActionErrors errors=new ActionErrors();
if(this.username==null||"".equals(username))...{
errors.add("username",new ActionError("username.null"));
}
if(this.password==null||"".equals(password))...{
errors.add("password",new ActionError("password.null"));
}
return errors;
}
public void reset()...{}
}
LoginAction.java
package app;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class LoginAction extends Action...{
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)...{
String username=((LoginForm)form).getUsername();
String password=((LoginForm)form).getPassword();
if(username.equals("sbx")&&password.equals("123"))...{
return mapping.findForward("success");
}else...{
return mapping.findForward("failure");
}
}
}
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class LoginAction extends Action...{
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)...{
String username=((LoginForm)form).getUsername();
String password=((LoginForm)form).getPassword();
if(username.equals("sbx")&&password.equals("123"))...{
return mapping.findForward("success");
}else...{
return mapping.findForward("failure");
}
}
}
资源文件:ApplicationResources.properties
# Resources for parameter 'app.ApplicationResources'
# Project struts
username.null=<li>Username is required</li>
password.null=<li>Password is required</li>
# Project struts
username.null=<li>Username is required</li>
password.null=<li>Password is required</li>
配置文件:struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<data-sources />
<form-beans>
<form-bean name="loginForm" type="app.LoginForm"/>
</form-beans>
<global-exceptions />
<global-forwards/>
<action-mappings>
<action path="/Login" type="app.LoginAction" name="loginForm" input="/login.jsp">
<forward name="success" path="/index.jsp"></forward>
<forward name="failure" path="/login.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="app.ApplicationResources" />
</struts-config>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<data-sources />
<form-beans>
<form-bean name="loginForm" type="app.LoginForm"/>
</form-beans>
<global-exceptions />
<global-forwards/>
<action-mappings>
<action path="/Login" type="app.LoginAction" name="loginForm" input="/login.jsp">
<forward name="success" path="/index.jsp"></forward>
<forward name="failure" path="/login.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="app.ApplicationResources" />
</struts-config>
validate="true"属性可有可无但一定得有input属性,否则报500错误
- struts简单验证
- struts--->简单数据验证
- struts中validate简单验证
- struts表单验证简单实现
- Struts 2 简单数据验证
- Struts验证框架的简单应用
- Struts+Servlet简单实现登录验证码
- struts验证fieldError actionError简单总结
- struts验证fieldError actionError简单总结
- struts验证fieldError actionError简单总结
- Struts(简单的Form验证)
- struts验证
- 应用struts的validator验证的一个简单实例
- struts中使用validator进行简单表单验证
- Struts Servlet简单实现登录随机验证码
- Struts的验证--Validator
- struts中的验证
- Struts Validator验证框架
- 现在计算机专业学生真的浮夸了(转)
- 使用JavaScript调用WebService的示例
- VS2003程序转到VS2005之后XP风格丢失的解决办法
- vc++ xml
- 12种让你的Apache配置更安全的方法
- struts简单验证
- 深入浅出HOOK API及完美应用
- 《厚黑完全手册》改良版
- 我对HGAME的看法(GalGame)
- PicVew与搜索引擎
- javaScript 对象大全
- c#.net常用函数和方法集
- 父母一辈子把我们培养成人不容易!我们绝不应该对他们说出以下的十句话
- 一位农村父亲对城市儿子的训诫