struts2中validate的用法,struts2validate

來源:互聯網
上載者:User

struts2中validate的用法,struts2validate

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;@Overridepublic String execute() throws Exception {System.out.println("checkuser");// TODO Auto-generated method stubreturn SUCCESS;}/*注意,validate方法是在execute之前執行*/@Overridepublic void validate() {user.setUsername(this.getUsername());user.setPassword(this.getPassword());System.out.println(user);// TODO Auto-generated method stubif(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用法

你用字串接受參數,然後轉換成Double類型,看看出不出異常,出異常表明不是數字。
或者直接用Regex驗證。或者乾脆直接在前台驗證,或者用struts2提供的驗證架構來驗證。
 
對於struts2中的validate

添加的錯誤資訊都在當前的Action對象上吧
在validate一般都是用this.addFieldError()這個方法嗎,thiis就是當前Action對象
除了用標籤,還可以用EL運算式等取,比如:${errors['錯誤欄位名']},這個可以指定取出哪個錯誤訊息
最後對於INPUT,在別的頁面也能取出來,這個可能是Struts2的一種機制吧,後台Action對象傳給前台頁面
希望對你有協助
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.