• 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之后。会发现例如以下的页面


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

  • 相关阅读:
    python_基础2
    springboot自定义配置文件类
    自定义实现spring-boot-starter-data-redis
    @ConditionalOnBean详解
    @Conditional详解
    并发包大神Doug Lea
    idea新建springboot项目
    springboot实现自定义start
    世界上唯一公平的事情就是每个人都会死。
    hashtable存null会发生什么?
  • 原文地址:https://www.cnblogs.com/yxwkf/p/4812159.html
Copyright © 2020-2023  润新知