FORM驗證簡單demo

來源:互聯網
上載者:User

詳細解釋稍後添加。

        項目結構

      


              web.xml

            

<?xml version="1.0" encoding="UTF-8" ?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="        http://java.sun.com/xml/ns/j2ee  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd    ">    <!-- 初始化日誌系統 -->    <listener>        <listener-class>com.alibaba.citrus.logconfig.LogConfiguratorListener</listener-class>    </listener>    <!-- 裝載/WEB-INF/webx.xml, /WEB-INF/webx-*.xml -->    <listener>        <listener-class>com.alibaba.citrus.webx.context.WebxContextLoaderListener</listener-class>    </listener>    <filter>        <filter-name>mdc</filter-name>        <filter-class>com.alibaba.citrus.webx.servlet.SetLoggingContextFilter</filter-class>    </filter>    <filter>        <filter-name>webx</filter-name>        <filter-class>com.alibaba.citrus.webx.servlet.WebxFrameworkFilter</filter-class>        <init-param>            <param-name>excludes</param-name>            <param-value><!-- 需要被“排除”的URL路徑,以逗號分隔,如/static, *.jpg。適合於映射靜態頁面、圖片。 --></param-value>        </init-param>        <init-param>            <param-name>passthru</param-name>            <param-value><!-- 需要被“略過”的URL路徑,以逗號分隔,如/myservlet, *.jsp。適用於映射servlet、filter。                對於passthru請求,webx的request-contexts服務、錯誤處理、開發模式等服務仍然可用。 --></param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>mdc</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <filter-mapping>        <filter-name>webx</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping></web-app>
webx.xml

<?xml version="1.0" encoding="UTF-8" ?><!-- Webx Root Context Configuration.  --><beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:services="http://www.alibaba.com/schema/services"    xmlns:request-contexts="http://www.alibaba.com/schema/services/request-contexts"    xmlns:session-encoders="http://www.alibaba.com/schema/services/request-contexts/session/encoders"    xmlns:model-encoders="http://www.alibaba.com/schema/services/request-contexts/session/model-encoders"    xmlns:session-idgens="http://www.alibaba.com/schema/services/request-contexts/session/idgens"    xmlns:session-stores="http://www.alibaba.com/schema/services/request-contexts/session/stores"    xmlns:ml-adapters="http://www.alibaba.com/schema/services/module-loader/adapters"    xmlns:ml-factories="http://www.alibaba.com/schema/services/module-loader/factories"    xmlns:beans="http://www.springframework.org/schema/beans"    xmlns:p="http://www.springframework.org/schema/p"    xsi:schemaLocation="        http://www.alibaba.com/schema/services http://localhost:8080/schema/services.xsd        http://www.alibaba.com/schema/services/request-contexts http://localhost:8080/schema/services-request-contexts.xsd        http://www.alibaba.com/schema/services/request-contexts/session/encoders http://localhost:8080/schema/services-request-contexts-session-encoders.xsd        http://www.alibaba.com/schema/services/request-contexts/session/idgens http://localhost:8080/schema/services-request-contexts-session-idgens.xsd        http://www.alibaba.com/schema/services/request-contexts/session/stores http://localhost:8080/schema/services-request-contexts-session-stores.xsd        http://www.alibaba.com/schema/services/request-contexts/session/model-encoders http://localhost:8080/schema/services-request-contexts-session-model-encoders.xsd        http://www.alibaba.com/schema/services/module-loader/adapters http://localhost:8080/schema/services-module-loader-adapters.xsd        http://www.alibaba.com/schema/services/module-loader/factories http://localhost:8080/schema/services-module-loader-factories.xsd        http://www.springframework.org/schema/beans http://localhost:8080/schema/www.springframework.org/schema/beans/spring-beans.xsd    ">    <!-- 支援${xxx}替換。 -->    <services:property-placeholder>        <services:property key="component">common</services:property>    </services:property-placeholder>    <!-- 共用配置。 -->    <beans:import resource="common/webx-component-and-root.xml" />    <!-- 異常管道。 -->    <beans:import resource="common/pipeline-exception.xml" />    <!-- 資源裝載。 -->    <beans:import resource="common/resources.xml" />    <!-- URI產生。 -->    <beans:import resource="common/uris.xml" />    <!-- 綜合設定。 -->    <services:webx-configuration>        <!-- 預設將productionMode設為true,建議在jetty外掛程式中設定-DproductionMode=false。 -->        <services:productionMode>${productionMode:true}</services:productionMode>        <services:components defaultComponent="app1" />    </services:webx-configuration>    <!-- 設定request/response/session。 -->    <services:request-contexts xmlns="http://www.alibaba.com/schema/services/request-contexts">        <basic />        <buffered />        <lazy-commit />        <parser />        <set-locale defaultLocale="zh_CN" defaultCharset="UTF-8" />        <session>            <id>                <cookie path="/" maxAge="0" httpOnly="true" />            </id>            <stores>                <session-stores:cookie-store id="temporaryCookie">                    <session-stores:cookie name="tmp" />                </session-stores:cookie-store>            </stores>            <store-mappings>                <match name="*" store="temporaryCookie" />            </store-mappings>        </session>    </services:request-contexts>    <!-- 支援上傳檔案。 -->    <services:upload sizeMax="5M" />    <!-- 將beans暴露給模板。這裡定義的tools可被所有components之間共用。 -->    <services:pull xmlns="http://www.alibaba.com/schema/services/pull/factories">        <utils />        <page-tool />        <control-tool />        <uris-tool />    </services:pull>    <!-- 裝載模組。 -->    <services:module-loader>        <ml-factories:class-modules>            <ml-factories:search-packages type="$1" packages="com.alibaba.webx.tutorial1.common.module.*" />        </ml-factories:class-modules>    </services:module-loader></beans:beans>

webx-app1.xml

<?xml version="1.0" encoding="UTF-8" ?><!-- Webx Sub Context Configuration.  --><beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:services="http://www.alibaba.com/schema/services"    xmlns:ml-adapters="http://www.alibaba.com/schema/services/module-loader/adapters"    xmlns:ml-factories="http://www.alibaba.com/schema/services/module-loader/factories"    xmlns:beans="http://www.springframework.org/schema/beans"    xmlns:p="http://www.springframework.org/schema/p"    xsi:schemaLocation="        http://www.alibaba.com/schema/services http://localhost:8080/schema/services.xsd        http://www.alibaba.com/schema/services/module-loader/adapters http://localhost:8080/schema/services-module-loader-adapters.xsd        http://www.alibaba.com/schema/services/module-loader/factories http://localhost:8080/schema/services-module-loader-factories.xsd        http://www.springframework.org/schema/beans http://localhost:8080/schema/www.springframework.org/schema/beans/spring-beans.xsd    ">    <!-- 支援${xxx}替換。 -->    <services:property-placeholder>        <services:property key="component">app1</services:property>    </services:property-placeholder>    <!-- 共用配置。 -->    <beans:import resource="common/webx-component-and-root.xml" />    <beans:import resource="common/webx-component.xml" />    <!-- 執行管道。 -->    <beans:import resource="common/pipeline.xml" />    <!-- 表單驗證。 -->    <beans:import resource="app1/form.xml" />    <!-- 裝載模組。 -->    <services:module-loader>        <ml-factories:class-modules>            <ml-factories:search-packages type="$1" packages="com.alibaba.webx.tutorial1.app1.module.*" />        </ml-factories:class-modules>    </services:module-loader></beans:beans>

default.xml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head>#showHead ("My Simple Webx Application")</head><body #bodyAttributes ()><p> This is default</p>$screen_placeholder<hr/><p>[<a href="$app1Link.setTarget("index")">Home</a>] [<a href="$app1Link">Dev Home</a>]</p></body></html>
hello.vm

$page.setTitle("Hello, $name")<p>Hello, $name!</p>

index.vm

<form action="$app1Link.setTarget("index")" method="post">  $csrfToken.hiddenField  <input type="hidden" name="action" value="simple_action"/>    <p>Hello, what's your name?</p>     #set($groupB = $form.simple.defaultInstance)             <input type="text" name="$groupB.name.key" value="$!groupB.name.value"/><input type="text" name="$groupB.password.key" value="$!groupB.password.value"/>  <input type="submit" name="event_submit_do_greeting"/> </form> 

login.vm

$page.setTitle("Hello, $name")<p>${name} is not valid</p>

form.xml

<?xml version="1.0" encoding="UTF-8"?><beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:services="http://www.alibaba.com/schema/services"    xmlns:fm-conditions="http://www.alibaba.com/schema/services/form/conditions"    xmlns:fm-validators="http://www.alibaba.com/schema/services/form/validators"    xmlns="http://www.alibaba.com/schema/services/form/validators"    xmlns:beans="http://www.springframework.org/schema/beans"    xmlns:p="http://www.springframework.org/schema/p"    xsi:schemaLocation="        http://www.alibaba.com/schema/services http://localhost:8080/schema/services.xsd        http://www.alibaba.com/schema/services/form/conditions http://localhost:8080/schema/services-form-conditions.xsd        http://www.alibaba.com/schema/services/form/validators http://localhost:8080/schema/services-form-validators.xsd        http://www.springframework.org/schema/beans http://localhost:8080/schema/www.springframework.org/schema/beans/spring-beans.xsd    ">    <services:form postOnlyByDefault="true">        <!--          - ===============================================          - 用來檢查csrf token。          - ===============================================         -->        <services:group name="csrfCheck">            <services:field name="csrfToken">                <csrf-validator>                    <message>提交的資料已到期</message>                </csrf-validator>            </services:field>        </services:group>        <!--          - ===============================================          - Simple form          - ===============================================         -->        <services:group name="simple" extends="csrfCheck">            <services:field name="name" displayName="你的名字">                <required-validator>                    <message>必須填寫 ${displayName}</message>                </required-validator>            </services:field>            <services:field name="password" displayName="你的密碼">                <required-validator>                    <message>必須填寫 ${displayName}</message>                </required-validator>            </services:field>        </services:group>    </services:form></beans:beans>

simpleAction.java

/* * Copyright 2010 Alibaba Group Holding Limited. * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *    http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package com.alibaba.webx.tutorial1.app1.module.action;import java.util.List;import com.alibaba.citrus.turbine.Context;import com.alibaba.citrus.turbine.Navigator;import com.alibaba.citrus.turbine.dataresolver.FormGroup;import com.alibaba.webx.tutorial1.app1.SimpleObject;public class SimpleAction {    public void doGreeting(@FormGroup("simple") SimpleObject simple, Navigator nav) throws Exception {             String name = simple.getName();        String password=simple.getPassword();               if("LTianchao".equals(name)&&("123".equals(password))){        nav.redirectTo("app1Link").withTarget("hello").withParameter("name", name);                }        else       nav.redirectTo("app1Link").withTarget("login").withParameter("name", name);        System.out.println("action begin");    System.out.println("done");        //、、context.put("name",simple[1].getName());        }}

hello.java

/* * Copyright 2010 Alibaba Group Holding Limited. * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *    http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package com.alibaba.webx.tutorial1.app1.module.screen;import com.alibaba.citrus.turbine.Context;import com.alibaba.citrus.turbine.dataresolver.Param;public class Hello {    public void execute(@Param("name") String name, Context context) {        context.put("name", name);    }}
login.java
/* * Copyright 2010 Alibaba Group Holding Limited. * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *    http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package com.alibaba.webx.tutorial1.app1.module.screen;import com.alibaba.citrus.turbine.Context;import com.alibaba.citrus.turbine.dataresolver.Param;public class login {    public void execute(@Param("name") String name, Context context) {        context.put("name", name);    }}

SimleObject.java

/* * Copyright 2010 Alibaba Group Holding Limited. * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *    http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package com.alibaba.webx.tutorial1.app1;public class SimpleObject {    private String name;    private String password;            public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }  }



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.