• struts2简单数据验证


    当表单数据提交到后台后通常要对数据进行校验,以登录为例,后台拿到用户名密码后会判断是否正确,正确的话会跳转到网站用户登录成功的页面,如果不正确的话会提示用户输入不正确。

    首先在struts.xml配置登录的action:

    <package name="user" namespace="/user" extends="struts-default">
             <action name="login" class="cn.orlion.user.UserAction" method="login">
                 <result name="error">
                     /login.jsp
                 </result>
                 <result name="success">
                     /backend.jsp
                 </result>
             </action>
         </package>

    然后创建一个UserAction(包:cn.orlion.user):

    package cn.orlion.user;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class UserAction extends ActionSupport{
        
        private String username;
        
        private String password;
    
        public String login(){
            
            if (!username.equals("user")) {
                
                this.addFieldError("username" , "用户名错误!");
                return ERROR;
            }
            
            if (!password.equals("pwd")) {
                
                this.addFieldError("password" , "密码错误!");
                return ERROR;
            }
            
            return SUCCESS;
        }
    
        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;
        }
        
    }

    backend.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>网站后台</title>
    </head>
    <body>
        登录成功
    </body>
    </html>

    login.jsp代码:

      解释:<%@taglib uri="/struts-tags" prefix="s" %>这一句是添加struts2的标签库并把前缀设为s。

      <s:fielderror fieldName="username" theme="simple" />(fieldName指定错误域名 theme指定模式)这一句是输出UserAction指定的错误(this.addFieldError("username" , "用户名错误!")它的输出结果如下:

    <ul class="errorMessage">

    <li>
    <span>

    用户名错误!

    </span>
    </li>

    </ul>

    <s:property value="errors.username[0]" />这一句只会输出:用户名错误!

    <s:debug></s:debug>会在jsp页面增加一个[debug]的链接:

    点击后会出现:

    Value Stack Contents是一个存放value的栈,Property Name是值的名称,Property Value是值。<s:property value="errors.username[0]" />就会取到这个栈中的errors中的username[0]。(errors是map类型,一个username可以包含多个错误信息)


    <%@ 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=UTF-8">
    <title>登录</title>
    </head>
    <body>
        <form action="" method="post">
            <input type="text" name="username" /><br/>
            <s:fielderror fieldName="username" theme="simple" />
            <s:property value="errors.username[0]" />
            <input type="text" name="password" /><br/>
            <s:fielderror fieldName="password" theme="simple" />
            <s:property value="errors.password[0]" />
            <input type="submit" value="登录"    />
        </form>
        <s:debug></s:debug>
    </body>
    </html>
  • 相关阅读:
    logback-spring.xml配置文件详解
    SpringBoot-Controller接收参数的几种常用方式
    spring boot配置定时任务设置
    SpringCloud 配置文件 application.yml和 bootstrap.yml区别
    ajax/get请求
    ajax封装2
    ajax封装1
    楼层特效
    旋转动画
    联动动画
  • 原文地址:https://www.cnblogs.com/orlion/p/5014404.html
Copyright © 2020-2023  润新知