大家好,又见面了,我是全栈君,今天给大家准备了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之后。会发现例如以下的页面
版权声明:本文博主原创文章,博客,未经同意不得转载。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/117030.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...