1. 程式人生 > >第27講 .struts2驗證框架

第27講 .struts2驗證框架

Struts2 基於 Struts2 攔截器,為開發者提供了一套易用的驗證框架,並可擴充套件;一般的驗證都支援;
1複製HeadFirstStruts2chapter06 改名為:HeadFirstStruts2chapter07 ,web project settings,刪除所有的properties檔案,
2新建com.cruise.model 包,新建User類,屬性userName,name age ,E-mail ,homePage

package com.cruise.model;

public class User {

    private

 String userName;
    private String name;
    private int age;
    private String email;
    private String homePage;
    public String getUserName() {
       return
 userName;
    }
    public void setUserName(String userName) {
       this.userName = userName;
    }
    public String getName() {
       return
 name;
    }
    public void setName(String name) {
       this.name = name;
    }
    public int getAge() {
       return age;
    }
    public void setAge(int age) {
       this.age = age;
    }
    public String getEmail() {
       return email;
    }
    public void setEmail(String email) {
       this.email = email;
    }
    public String getHomePage() {
       return homePage;
    }
    public void setHomePage(String homePage) {
       this.homePage = homePage;
    }
    @Override
    public String toString() {
       return "User [userName=" + userName + ", name=" + name + ", age=" + age + ", email=" + email + ", homePage="
              + homePage + "]";
    }
}
3新建com.eruise.action包,和RegisterAction.繼承ActionSupport, user屬性,execute預設方法,
package com.cruise.action;

import com.cruise.model.User;
import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport{

    private User user;

    public User getUser() {
       return user;
    }

    public void setUser(User user) {
       this.user = user;
    }
    @Override
    public String execute() throws Exception {
       System.out.println("列印傳入的:"+user);
       return SUCCESS;
    }
}
4配置struts.xml檔案,如果驗證驗證框架被拒絕,會返回一個"input",
xml version="1.0" encoding="UTF-8" ?>
DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="manager" extends="struts-default">
       <action name="registerAction" class="com.cruise.action.UserAction">
           <result name="input">/register.jspresult>
           <result name="success">/success.jspresult>
       action>
    package>
struts>
5配置register.jsp 註冊頁面,和success.jsp 檔案,
register.jsp如下

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
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 heretitle>
head>
<body>
<s:form action="registerAction" method="post">
    <s:textfield name="user.userName" label="使用者名稱">s:textfield>
    <s:textfield name="user.name" label="真實姓名">s:textfield>
    <s:textfield name="user.age" label="年齡">s:textfield>
    <s:textfield name="user.email" label="郵箱">s:textfield>
    <s:textfield name="user.homePage" label="主頁">s:textfield>
    <s:submit value="提交">s:submit>
s:form>
body>
html>

success.jsp如下
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
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 heretitle>
head>
<body>
註冊成功!!!
table>
body>
html>
6在com.cruise.action包下,新建RegisterAction-validation.xml檔案,寫驗證器,對各個欄位進行條件驗證,
xml version="1.0" encoding="UTF-8"?>
DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN""http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
    <field name="user.userName">
       <field-validator type="requiredstring">
           <message>請輸入使用者名稱!!!message>
       field-validator>
       <field-validator type="stringlength">
           <param name="maxLength">20param>
           <param name="minLength">6param>
           <message>使用者名稱的長度必須在${minLength}和${maxLength}之間message>
       field-validator>
    field>
    <field name="user.name">
       <field-validator type="requiredstring">
           <message>請輸入真實姓名!!!message>
       field-validator>
    field>
    <field name="user.age">
       <field-validator type="int">
           <param name="min">18param>
           <message>年齡必須滿18週歲!!!message>
       field-validator>
    field>
    <field name="user.email">
    <field-validator type="requiredstring">
           <message>請輸入郵箱!!!message>
       field-validator>
       <field-validator type="email">
           <message>郵件格式不對!!!message>
       field-validator>
    field>
    <field name="user.homePage">
       <field-validator type="requiredstring">
           <message>請輸入主頁!!!message>
       field-validator>
       <field-validator type="url">
           <message>主頁格式不對!!!message>
       field-validator>
    field>
validators>
7測試
http://localhost:8080/HeadFirstStruts2chapter07/register.jsp
8驗證表示式,驗證使用者名稱和真實姓名不能相同,在RegisterAction-validation中,新增驗證表示式,在register.jsp 中新增驗證錯誤的提示程式碼
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN""http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
    <field name="user.userName">
       <field-validator type="requiredstring">
           <message>請輸入使用者名稱!!!</message>
       </field-validator>
       <field-validator type="stringlength">
           <param name="maxLength">20</param>
           <param name="minLength">6</param>
           <message>使用者名稱的長度必須在${minLength}和${maxLength}之間</message>
       </field-validator>
    </field>
    <field name="user.name">
       <field-validator type="requiredstring">
           <message>請輸入真實姓名!!!</message>
       </field-validator>
    </field>
    <field name="user.age">
       <field-validator type="int">
           <param name="min">18</param>
           <message>年齡必須滿18週歲!!!</message>
       </field-validator>
    </field>
    <field name="user.email">
    <field-validator type="requiredstring">
           <message>請輸入郵箱!!!</message>
       </field-validator>
       <field-validator type="email">
           <message>郵件格式不對!!!</message>
       </field-validator>
    </field>
    <field name="user.homePage">
       <field-validator type="requiredstring">
           <message>請輸入主頁!!!</message>
       </field-validator>
       <field-validator type="url">
           <message>主頁格式不對!!!</message>
       </field-validator>
    </field>
    <validator type="expression">
       <param name="expression"><![CDATA[!user.userName.equals(user.name)]]></param>
       <message>真實姓名和使用者名稱不能相同!!!</message>
    </validator>
</validators>
register.jsp檔案如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!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:actionerror/>
註冊
<s:form action="registerAction" method="post">
    <s:textfield name="user.userName" label="使用者名稱"></s:textfield>
    <s:textfield name="user.name" label="真實姓名"></s:textfield>
    <s:textfield name="user.age" label="年齡"></s:textfield>
    <s:textfield name="user.email" label="郵箱"></s:textfield>
    <s:textfield name="user.homePage" label="主頁"></s:textfield>
    <s:submit value="提交"></s:submit>
</s:form>
</body>
</html>
9測試
http://localhost:8080/HeadFirstStruts2chapter07/register.jsp