Struts2 input validation (3): override the validatexxx method (method 2)

Source: Internet
Author: User

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 ";}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.