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 框架的工作流程:


当所有请求被核心控制器FilterDispatcher拦截的时候,FilterDispatcher会将请求转发给action代理,action代理会根据配置文件struct.xml决定转发给哪一个action。将请求转发给action的过程中,又会经过一些拦截器,这些拦截器负责将请求解析后转发给相应的action。接着,经过相应action中execute()处理后,即得到一个视图结果,根据该结果并结合相应模板就可以产生相应的输出流。输出流再经过一些拦截器之后,传送到了浏览器。


三、分析利用structs2框架生成的 helloworld 项目

helloworld.jsp 页面的内容:
<%@ 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