structs 2.x 框架的初步学习
来源:互联网 发布:网络暴力事例2017 编辑:程序博客网 时间:2024/05/18 11:24
一、使用 NetBeans 8.0.2 建立 struct2.x 框架环境
新建Java web项目,在框架选择的时候,勾选structs2。点击完成,即会生成名为 helloworld 的项目。
注意: NetBeans 8.0.2 中默认包含struct 1.3.10插件。要使用structs2框架,需要自己下载并安装structs2插件。
二、分析 struct2.x 框架
struct2.x 框架由3个部分组成:核心控制器FilterDispatcher、业务控制器和业务逻辑组件。其中,核心控制器FilterDispatcher是由struct2.x 框架提供,而业务控制器和业务逻辑组件需要用户自己实现。3个部分的功能分别是:
核心控制器FilterDispatcher:负责拦截所有用户请求,如果用户的请求以action结尾,则该请求被转入 struct2.x 框架处理。
业务控制器:实现action类的实例,该类通常有一个能返回一个字符串形式的业务逻辑名的execute()方法,用于项目业务控制的实现。
业务逻辑组件:由 Javabean 或者 EJB 来实现,,用于项目业务逻辑的实现。
struct2.x 框架的工作流程:
三、分析利用structs2框架生成的 helloworld 项目
<%@ page contentType="text/html; charset=UTF-8" %><%@ taglib prefix="s" uri="/struts-tags" %><html> <head> <title><s:text name="HelloWorld.message"/></title> </head> <body> <h2><s:property value="message"/></h2> <h3>Languages</h3> <ul> <li> <s:url id="url" action="HelloWorld"> <s:param name="request_locale">en</s:param> </s:url> <s:a href="%{url}">English</s:a> </li> <li> <s:url id="url" action="HelloWorld"> <s:param name="request_locale">es</s:param> </s:url> <s:a href="%{url}">Espanol</s:a> </li> </ul> </body></html>在上面代码中,元素<s:url>的 action 属性值必须是 struct.xml 配置过的请求名,即HelloWorld。
struct.xml 内容:
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <!--使用example.xml配置文件--> <include file="example.xml"/> <!-- Configuration for the default package. --> <package name="default" extends="struts-default"> </package></struts>
example.xml 内容:
<?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> <!--创建名为example的包,该包必须继承默认包--> <package name="example" namespace="/example" extends="struts-default"> <!--配置helloworld请求action--> <action name="HelloWorld" class="example.HelloWorld"> <result>/example/HelloWorld.jsp</result> </action> </package></struts>元素<action>中属性name表示请求的名称(HelloWorld),class表示处理该请求的具体执行类(example.HelloWorld)。
HelloWorld.java 内容:
/* * $Id: HelloWorld.template,v 1.2 2008-03-27 05:47:21 ub3rsold4t Exp $ * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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 example;import com.opensymphony.xwork2.ActionSupport;/** * <code>Set welcome message.</code> */public class HelloWorld extends ActionSupport { public String execute() throws Exception { setMessage(getText(MESSAGE)); return SUCCESS; } /** * Provide default valuie for Message property. */ public static final String MESSAGE = "HelloWorld.message"; /** * Field for Message property. */ private String message; /** * Return Message property. * * @return Message property */ public String getMessage() { return message; } /** * Set Message property. * * @param message Text to display on HelloWorld page. */ public void setMessage(String message) { this.message = message; }}HelloWorld.java文件就是对应名为 HelloWorld 的action。上面代码,实现了相应的业务逻辑组件。
注意:HelloWorld.message 定义在package.properties和package_es.properties两个文件中。
项目运行结果:
0 0
- structs 2.x 框架的初步学习
- Structs的validator验证框架
- SSH框架入门学习之一(structs)
- Structs框架
- structs的学习之路
- cocos2d-x的初步学习一
- cocos2d-x的初步学习二
- cocos2d-x的初步学习三
- cocos2d-x的初步学习四
- cocos2d-x的初步学习五
- cocos2d-x的初步学习六
- cocos2d-x的初步学习七
- cocos2d-x的初步学习八
- cocos2d-x的初步学习九
- Structs学习笔记(一):Structs中标签的前缀
- Structs的validator验证框架(转)
- MyEclipse撤销已部署的Structs框架
- 我的第一个structs框架
- ECSHOP 广告添加搜索功能
- 【玩转cocos2d-x之三十二】xml的解析
- 单例模式的简单创建
- 获取android APP 签名的MD5和SHA1
- 【玩转cocos2d-x之三十三】游戏嵌入Webview网页
- structs 2.x 框架的初步学习
- volley 发送两次或者多次请求问题,解决方案
- Json的读写数据详解步骤
- 【玩转cocos2d-x之三十四】绘图:CCDrawingPrimitives和CCDrawNode
- Zxing 在Fragment中实现二维码扫描(不推荐使用)
- SOA架构 今天你实现了吗?
- 读书笔记:自定义函数实现字符串处理函数----strlen、strcmp、strcat、strcpy
- 【玩转cocos2d-x之三十五】Earth Warrior 3D大揭秘
- PAT 1051. Pop Sequence (25)