您的位置:首页 > 编程语言 > Java开发

Struts入门(一)

2016-01-08 00:00 627 查看
摘要: 本文快速带大家入手Struts项目

Jakarta Struts Framework 是一个开放源代码的 Apache 项目,由 Craig R. McClanahan 于 2000 年创建,并捐赠给了 Apache 基金会,已经成为基于 Java Servlet 和 Java Server Page (JSP)技术构建 Web 应用程序的最流行的框架包。 Craig 编写了 Tomcat4 实现的大部分内容,同时他还是 JCP 的资深成员,负责 Servlet 和 JSP 规范的制定。
Struts 是一个基于模型 (Model) -视图 (View) -控制器 (Controller)(MVC) 模式的应用架构的开源框架。如果您开发过大型 Web 应用程序,您就理解变化这个词的含义。“模型 - 视图 - 控制器” (MVC) 就是用来帮助您控制变化的一种设计模式。 MVC 减弱了业务逻辑接口和数据接口之间的耦合。
JSP 页面中可以非常容易地结合业务逻辑 (jsp:useBean) 、服务端处理过程( jsp:scriplet )和 HTML(<html>) ,在 JSP 页面中同时实现显示,业务逻辑和流程控制,从而可以快速地完成应用开发。现在很多的 Web 应用就是由一组 JSP 页面构成的。这种以 JSP 为中心的开发模型我们可以称之为 Model 1 。当然这种开发模式在进行快速和小规模的应用开发时,是有非常大的优势,但是从工程化的角度考虑,它也有一些不足之处,主要在于不方便应用扩展,以及不利 于应用系统业务的重用和改动。考虑到这些问题在开发大型的 Web 应用时必须采用不同的设计模式——这就是 Model2 。
Model 2 表示的是基于 MVC 模式的框架。 "Model" 代表的是应用的业务逻辑(通过 JavaBean , EJB 组件实现), "View" 是应用的表示面(由 JSP 页面产生), "Controller" 是提供应用的处理过程控制(一般是一个 Servlet ),通过这种设计模型把应用逻辑,处理过程和显示逻辑分成不同的组件实现,这些组件可以进行交互和重用。 Model 2 具有组件化的优点从而更易于实现对大规模系统的开发和管理。
Struts 是一组相互协作的类、 Servlet 和 JSP 标记,它们组成一个可重用的 MVC 2 设计。这个定义表示 Struts 是一个框架,而不是一个库,但 Struts 也包含了丰富的标记库和独立于该框架工作的实用程序类库。 Struts 利用 taglib 获得可重用代码和抽象 Java 代码,利用 ActionServlet 配合 Struts-config.xml 实现对整个系统导航。增强了开发人员对系统的整体把握,提高了系统的可维护性和可扩充性。
Struts 是一个 Web Framework ,常见的 B/S Web Framework 如下:
Struts :主要关注于 Controller 流程,基于 JSP与 Taglib 实现的 View 。
Cocoon :用于处理 XML 的框架,将 XML 作为 View 并可以良好结合 XSLT 。
Spring :这是一个服务于所有层面的 Application Framework ,提供了 bean 的配置基础, AOP 的支持, JDBC 的提
取框架,抽象事务支持等。它有一个非常显著的特点:在某个层面上如果你不需要 Spring 的支持,你就可以
不使用 String 的 class ,只使用它的某一部分的功能。
JSF :JavaServerFaces ,严格意义上讲,它并不算是 Framework ,但 JSF 同样提供了一种不同的 View 生成方式。
它可以和多种 Framework 整合。在 J2EE1.4 中, JSP2.0 规范就包含了 JSF 标准。
Framework 有很多种, Struts 并不是我们唯一的选择。我们可以根据具体的项目来选择不同的 Framework 。 Struts 之所以如此流行,其主要原因是它推出时间比较早,版本较稳定,并且是 MVC 及 core J2EE patterns 的标准实现。
Struts 师出名门,拥有诸多优点,其中最关键的一点就是简单易用,这也是正他迅速普及,越来越流行的根本原因。与其同时代的 Tubine 、 Cocoon 等,都可以作为 Web 框架,功能也很强大,但使用起来过于复杂,一直难以流行。作为一名普通的 Java 开发者,只要稍具 JSP 开发水平,都可以在一周左右的时间内,基本学会 Struts 。
Struts 还提供了丰富的 Taglib ,熟练用之,将节省大量的开发时间。
Struts 具有良好的页面导航功能,开发者可以通过一个名为 struts-config.xml 的文件,把握整个系统各部分之间的联系,这对于后期维护有着莫大的帮助,尤其是当另一批开发者接手该项目时,这种优势体现得更加明显。
当然, Struts 也有一些不足之处。对于很大的项目,当 struts-config.xml 越来越庞大时,维护也就变得越来越复杂,甚至需要专人负责(对于这种维护情况,建议采用专门的维护工具,如 Struts Console )。另外, Struts 框架主要是针对表示层设计的,对于后端的逻辑层支持不是很强。
将来的发展趋势,很有可能是 JSF+Struts+Spring+Hibernate/JDO 的开发模式,建议你从 Struts 入手。
话不多说开始入门

web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>StrutsDemo</display-name>

<!-- struts2框架的核心控制器,用户对本程序的所有请求都先交给 StrutsPrepareAndExecuteFilter核心控制器进行处理
核心控制器就是根据struts.xml的文件来决定具体使用什么业务逻辑控制器处理本次请求
-->
<filter>
<filter-name>struts</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts</filter-name>
<!-- 表示接收所有请求 -->
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>


这个是struts的模块配置,他被struts.xml   include放进模块 hr-user.xml,很多公司都是模块式开发,所以我也用模块是开发

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<package name="hr" extends="struts-default" namespace="/">
<action name="user" class="org.heinrich.action.UserAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>


这个是struts的全局配置,在这个xml中的所有配置,在其他的模块中都可以用相当于定义了全局变量,在其他模块中的就是局部变量

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<!-- 在struts.xml文件中通常是配置一下模块的引入配置和公共配置
公共的常量配置可以参考struts2-core-2.3.15.1.jar中的default.properties文件中配置
-->
<!-- 配置本项目允许的请求后缀 -->
<constant name="struts.action.extension" value="action,do,,"/>
<!-- 配置本工程是开发模式(会热部署) -->
<constant name="struts.devMode" value="true"/>
<!-- 开启动态方法调用,默认是开启状态 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>

<!-- 为了方便团队分模块开发,通常建议将不同的模块的包(package)对应的单独的文件中
然后使用include 标签导入的struts.xml文件中
-->
<include file="hr-user.xml"/>
</struts>


UserAction 的配置

package org.heinrich.action;

import org.heinrich.dao.UserDao;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

private static final long serialVersionUID = 6161139006224338587L;
private String key;
private String value;

public String add() {

System.out.println("I am reading Book about Linux");

UserDao userDao = new UserDao();
userDao.add(key, value);
return "success";
}

public String delete() {

System.out.println("I am reading Book about Linux");

UserDao userDao = new UserDao();
userDao.delete(key);
return "success";
}

public String update() {

System.out.println("I am reading Book about Linux");

UserDao userDao = new UserDao();
userDao.update(key, value);
return "success";
}

public String get() {

System.out.println("I am reading Book about Linux");

UserDao userDao = new UserDao();
String name = userDao.get(key);
System.out.println(name);
return "success";
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

}


定义了CURD四个方法,所有方法都采用
http://localhost:8080/Struts/user!add?key="color"&value="bule" http://localhost:8080/Struts/user!delete?key="null" http://localhost:8080/Struts/user!update?key="color"&value="red" http://localhost:8080/Struts/user!update?key="color"
这种模式进行访问,这样可以省去很多代码,当然你也可以有其他的方式,比如动态方法调用,那样需要修改局部的hr-user.xml的代码

现在我配置给大家看 http://localhost:8080/Struts/user_add?key="color"&value="bule" http://localhost:8080/Struts/user_delete?key="null" http://localhost:8080/Struts/user_update?key="color"&value="red" http://localhost:8080/Struts/user_update?key="color"


hr-user.xml的配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="hr" extends="struts-default" namespace="/">
<action name="user_*" class="org.heinrich.action.UserAction" method="{1}">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>


其实还有很多中配置方式,最简单的方式就是

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<package name="hr" extends="struts-default" namespace="/">

<action name="add" class="org.heinrich.action.UserAction" method="add">
<result name="success">/success.jsp</result>
</action>
<action name="delete" class="org.heinrich.action.UserAction" method="delete">
<result name="success">/success.jsp</result>
</action>
<action name="update" class="org.heinrich.action.UserAction" method="update">
<result name="success">/success.jsp</result>
</action>
<action name="get" class="org.heinrich.action.UserAction" method="get">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>


请求URL是

http://localhost:8080/Struts/add?key="color"&value="bule" http://localhost:8080/Struts/delete?key="null" http://localhost:8080/Struts/update?key="color"&value="red" http://localhost:8080/Struts/update?key="color"[/code] 
这个看起来是不是很多,但是他是最简单的,每个方法对应一个action,所以说公司很多都是用第一种或者第二种

下次我会写关于Struts2怎么接收请求参数,和获取Request的几个方式,和Struts2底层运行的情况,带大家Review一次源代码,

求关注,求转发
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: