zoukankan      html  css  js  c++  java
  • struts2于validate要使用

    package com.test.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    import com.test.model.User;
    
    public class CheckUser extends ActionSupport{
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	
    	private User user=new User();
    	private String username;
    	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;
    	}
    
    
    	private String password;
    
    	@Override
    	public String execute() throws Exception {
    		System.out.println("checkuser");
    		// TODO Auto-generated method stub
    		
    		
    		return SUCCESS;
    	}
    
    
    	/*注意,validate方法是在execute之前运行*/
    	@Override
    	public void validate() {
    		user.setUsername(this.getUsername());
    		user.setPassword(this.getPassword());
    		System.out.println(user);
    		// TODO Auto-generated method stub
    		if(user.getUsername().equals("admin"))
    		{
    			this.addFieldError("username_error", "权限不够");
    		}
    		super.validate();
    	}
    	
    
    }


    struts.xml

    <?xml version="1.0" encoding="UTF-8" ?

    > <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 採用注解的方式 --> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="" extends="struts-default"> <!-- struts2捕获action类异常 --> <!-- <global-results> <result name="error">/error.jsp</result> </global-results> <global-exception-mappings> <exception-mapping result="error" exception="java.lang.Exception"></exception-mapping> </global-exception-mappings> --> <action name="link" class="com.test.action.LoginAction"> <result>/result.jsp</result> </action> <action name="linkIOC" class="com.test.action.LoginActionIOC"> <result>/result.jsp</result> </action> <!-- 正确和错误的分别跳转,齐总input是在验证失败后会跳转的页面 --> <action name="data" class="com.test.action.CheckUser"> <result name="success">/result.jsp</result> <result name="input">/login.jsp</result> </action> </package> <!-- Add packages here --> </struts>


    html页面

    <%@ 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>Insert title here</title>
    </head>
    <body>
    <!-- 这个地方是用来显示错误信息 -->
    <s:fielderror/>
    <form action="data.action" method="post">
    <input type="text" name="username"><br>
    <input type="password" name="password"><br>
    <input type="submit" name="ok"><br>
    </form>
    </body>
    </html>

    即当严重不通过后,会返回到该页面,同一时候输出出错信息。

    測试结果。当输入admin之后。会发现例如以下的页面


    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    suse12安装详解
    Centos7上部署openstack mitaka配置详解(将疑难点都进行划分)
    菜鸟帮你跳过openstack配置过程中的坑[文末新添加福利]
    openstack中dashboard页面RuntimeError: Unable to create a new session key. It is likely that the cache is unavailable.
    Multiple network matches found for name 'selfservice', use an ID to be more specific.报错
    查看 SELinux状态及关闭SELinux
    SELinux深入理解
    IP地址、子网掩码、网络号、主机号、网络地址、主机地址
    Oracle job procedure 存储过程定时任务
    POI文件导出至EXCEL,并弹出下载框
  • 原文地址:https://www.cnblogs.com/yxwkf/p/4812159.html
Copyright © 2011-2022 走看看