struts2于validate要使用

struts2于validate要使用

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

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

struts2于validate要使用

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/117030.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • mathtype大括号三个公式_latex大括号左对齐

    mathtype大括号三个公式_latex大括号左对齐数学方程输入对于很多理工科朋友来说是一件让人头疼的事情。不仅是数学方程本身就纷繁复杂花样百出,各种输入语法更是劝退无数人。然而很多看似复杂的东西其实并非如想象中的难,抓住本质即可快速入门。今天和大家分享下如何快速入门LaTeX,在XMind2020中轻松输入方程。01LaTeX是什么LaTeX是一种基于ΤΕΧ的排版系统,具有方便而强大的数学公式排版能力。日常中大家常见的高印刷质量的…

    2022年10月10日
  • SQL%NOTFOUND_oracle导出sql文件

    SQL%NOTFOUND_oracle导出sql文件SQL%NOTFOUND是一个布尔值。与最近的sql语句(update,insert,delete,select)发生交互,当最近的一条sql语句没有涉及任何行的时候,则返回true。否则返回fal

  • spdlog 日志库学习,简易封装

    spdlog 日志库学习,简易封装spdlogwiki:https://github.com/gabime/spdlog/wiki别人的学习笔记:https://www.cnblogs.com/oucsheep/p/8426548.html别人的学习笔记:https://github.com/gabime/spdlog/wiki百度搜spdlog封装可以看到很多写的差不多的单例类,我看公司遗留的代码也是借鉴这些写的。最常见的是一开头就写上:#ifdef_WIN32#define__FILENAME__(s

  • disruptor框架原理_disruptor使用

    disruptor框架原理_disruptor使用Disruptor源码https://github.com/LMAX-Exchange/disruptor/blob/master/README.mdhttps://github.com/LMAX-Exchange/disruptor/wiki/Introductionhttps://github.com/LMAX-Exchange/disruptor/wiki/Getting-Starte…

  • Spring日志管理

    Spring日志管理SpringBoot关于日志的官方文档1、简述SpringBoot官方文档关于日志的整体说明本博客基于SpringBoot_1.3.6大家请先简单看下这篇英文的官方文档,文中有说 SpringBoot 内部日志系统使用的是 CommonsLogging 并且 SpringBoot 给 JDKLogging , Log4j2(Log4j也是支持的) , Lo

  • Enterprise Library概述

    Enterprise Library概述Webcast网络讲座:企业库系统课程企业库(EnterpriseLibrary)是微软的模式与实践(Patterns&Practices)的下一代应用程序块(ApplicationBlocks)。该企业库的设计思想是为了协助开发商解决企业级应用开发过程中所面临的一系列共性的问题,如安全(Security)、日志(Logging)、数据访问(DataA…

    2022年10月20日

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号