详解稍后加入。
项目结构如图:
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; } }