struts2通过类.properties文件验证
来源:互联网 发布:京东大数据是什么 编辑:程序博客网 时间:2024/05/27 01:42
1:login.jsp
<%@ page language="Java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
< %@ taglib prefix="s" uri="/struts-tags"%>
< !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=UTF-8">
< title>Insert title here</title>
< /head>
< body>
<form action="islogin3" method="post">
用户名:<input type="text" name="username"/>
<%-- <s:fielderror fieldName="username"></s:fielderror> --%>
<font color="red">${fieldErrors.username[0]}</font><br/>
密码:<input type="password" name="pwd"/>
<font color="red">${fieldErrors.pwd[0]}</font><br/>
<%-- <s:fielderror fieldName="pwd"></s:fielderror> --%>
<input type="submit" value="登陆"/><br/>
</form>
<s:debug></s:debug>
< /body>
< /html>
2:web.xml
<?xml version="1.0" encoding="UTF-8"?>
< web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://Java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<!-- 配置 Struts2 的 Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>struts.i18n.encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>/day01/welcome.jsp</welcome-file>
</welcome-file-list>
< /web-app>
3:struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
< !DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
< struts>
<!-- 一旦Struts.xml文件修改后就自动重新加载 -->
<constant name="struts.configuration.xml.reload" value="true"></constant>
<!--默认视图 -->
<constant name="struts.ui.theme" value="simple"></constant>
<!-- 编码格式 -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!-- 配置 Struts 可以受理的请求的扩展名 -->
<constant name="struts.action.extension" value="action,do,"></constant>
<!-- 允许ognl静态访问资源 -->
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
<!-- 设置是否支持动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<package name="hellodemo" extends="struts-default" namespace="/">
<default-action-ref name="welcome"></default-action-ref>
<!-- 配置 全局结果集-->
<global-results>
<result name="input">/day01/login.jsp</result>
<result name="error">/day01/error.jsp</result>
</global-results>
<!-- 配置全局异常 -->
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.NULLPointerException"></exception-mapping>
</global-exception-mappings>
<action name="findalluser" class="cn.itcast.action.User1Action" method="findalluser">
<result name="success">/day03/showTag.jsp</result>
</action>
<action name="islogin2" class="cn.itcast.action.LoginAction" method="checklogin">
<result name="suc">/day02/suc.jsp</result>
<result name="input">/day02/index_login.jsp</result>
</action>
<action name="islogin3" class="cn.itcast.action.User1Action" method="islogin">
<result name="success">/day03/success.jsp</result>
<!-- struts2验证不通过转移的页面 -->
<result name="input">/day03/login.jsp</result>
</action>
<action name="hello" class="cn.itcast.action.HelloAction" method="execute">
<result name="success">/hello.jsp</result>
</action>
<action name="upload" class="cn.itcast.action.UploadFileAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
<action name="islogin" class="cn.itcast.action.UserAction" method="islogin">
<!-- action中的方法执行完后不直接返回jsp页面,而是执行另一个action中的方法 -->
<result name="success" type="chain">findsomeinfo</result>
</action>
<action name="isreg" class="cn.itcast.action.UserAction" method="isreg">
<result name="success">/day01/regsuc.jsp</result>
</action>
<action name="findsomeinfo" class="cn.itcast.action.UserAction" method="findinfo">
<result name="success">/day01/success.jsp</result>
</action>
<action name="welcome">
<result>/day01/welcome.jsp</result>
</action>
</package>
< /struts>
4:UserAction.properties
usernameisnull=\u7528\u6237\u540D\u4E0D\u80FD\u4E3A\u7A7A-useraction(用户名不能为空)
pwdisnull=\u5BC6\u7801\u4E0D\u80FD\u4E3A\u7A7A-useraction(密码不能为空)
5:UserAction
package cn.itcast.action;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
//Struts2验证时必须继承ActionSupport,重写validate方法
public class User1Action extends ActionSupport{
private static final long serialVersionUID = 1L;
private List<Users> userlist;
private String username;
private String pwd;
public String findalluser(){
System.out.println("findalluser...");
userlist=new ArrayList<Users>();
Users user1=new Users(1001,"zhangsan","123","张三");
Users user2=new Users(1002,"lisi","234","李四");
Users user3=new Users(1003,"wangwu","345","王五");
userlist.add(user1);
userlist.add(user2);
userlist.add(user3);
return "success";
}
//struts2框架先执行validate方法,如果验证失败,则不会在执行其它方法,直接返回"input"(每次请求都执行)
// @Override
// public void validate(){
// if(username==null||username.equals("")){
// super.addFieldError("username", "用户名不能为空!");//添加错误信息
// }if(pwd==null||pwd.equals("")){
// super.addFieldError("pwd", "密码不能为空!");
// }
// super.validate();
// }
//正在登陆时执行验证
public void validateIslogin(){
if(username==null||username.equals("")){
//super.addFieldError("username", "用户名不能为空!");//添加错误信息
super.addFieldError("username", super.getText("usernameisnull"));//从配置文件中获取错误信息
}if(pwd==null||pwd.equals("")){
//super.addFieldError("pwd", "密码不能为空!");
super.addFieldError("pwd", super.getText("pwdisnull"));//从配置文件中获取错误信息
}
super.validate();
}
public String islogin()
{
System.out.println("执行islogin登陆验证...");
System.out.println("接收到页面提交的username:"+username+",pwd:"+pwd);
return "success";
}
public List<Users> getUsers() {
return userlist;
}
public void setUsers(List<Users>userlist) {
this.userlist = userlist;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
6:success.jsp
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
< !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=UTF-8">
< title>Insert title here</title>
< /head>
< body>
< h3>登陆成功</h3>
< /body>
< /html>
- struts2通过类.properties文件验证
- Struts2 Struts.properties文件
- Struts2的properties文件
- Struts2的properties文件
- 通过properties类 读取文件内容
- 通过Properties读写文件
- struts2 properties文件取值
- Struts2的struts.properties文件
- 通过.properties文件配置参数
- Properties类与.properties文件
- Properties类与.properties文件
- Struts2的default.properties和struts.properties文件
- struts2 属性文件struts2.properties文件的所有设置项
- struts2的struts.properties文件详解
- struts2的struts.properties文件详解
- Struts2读取自定义配置Properties文件
- Struts2 文件上传struts.properties配置
- Struts2的struts.properties文件在哪儿啊?
- UpdateData()用法
- UVA11988 Broken Keyboard (a.k.a. Beiju Text)【输入输出】
- js+ant版本控制,解决js、css缓存问题
- 常量
- 矩阵、向量求导---Matrix calculus
- struts2通过类.properties文件验证
- 寒冰王座
- 如何进行高效迅速的CodeReview
- Bad operand types for binary operator '&&'
- Eclipse之打开就卡死-yellowcong
- 各个无人超市品牌使用的技术!
- hibernate 使用和查询
- USACO Dec. 09 过路费
- Scala Map[K, V]