As we know, the action class of struts2 can contain multiple processing logics. Different Processing logics correspond to different methods. That is, several methods similar to execute are defined in the action class of struts2, but the method name is not execute.
If we only want to validate a processing logic for input validation, that is, simply verifying a processing method, it is obviously not appropriate to rewrite the validate method, because if we rewrite the validate method, no matter which method the action is called, verification is triggered. This is obviously not what we want to see. The solution at this time is: If you only need to verify the xxx method, rewrite the validatexxx method.
Index. jsp:
<% @ Page contenttype = "text/html; charset = UTF-8 "pageencoding =" UTF-8 "%> <% @ taglib prefix =" S "uri ="/Struts-tags "%> <HTML>
Welcome. jsp:
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
Struts. xml:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"><struts> <package name="demo" extends="struts-default"> <action name="*_Action" class="action.RegisterAndLoginAction" method="{1}"> <result name="register_success">/index.jsp</result> <result name="login_success">/welcome.jsp</result> <result name="input">/index.jsp</result> <result name="error">/index.jsp</result> </action> </package></struts>
Registerandloginaction. Java:
Public class registerandloginaction extends actionsupport {private string name; private string pass; Public String getname () {return name;} public void setname (string name) {This. name = Name;} Public String getpass () {return pass;} public void setpass (string pass) {This. pass = pass;} public void validateregister () {system. out. println ("Enter the validateregister () method... "); If (name. equals ("") | Name = NULL) {This. addfielderror ("name1", "name cannot be blank");} If (name. length () <3 | Name. length ()> 25) {This. addfielderror ("name2", "the length of an English name cannot be less than 3 or greater than 25");} If (Pass. equals ("") | pass = NULL) {This. addfielderror ("pass1", "password cannot be blank");} If (Pass. length () <3 | pass. length ()> 25) {This. addfielderror ("pass2", "the password length cannot be less than 3 or greater than 25");} Public String login () {If (name. equals ("Scott") & pass. equals ("tiger") {actioncontext. getcontext (). getsession (). put ("name", name); Return "login_success";} This. addactionerror ("incorrect user name or password"); Return "error";} Public String register () {If (name. equals ("Scott") & pass. equals ("tiger") {return "register_success" ;}return "error ";}}