博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Struts2--Helloworld
阅读量:7041 次
发布时间:2019-06-28

本文共 5589 字,大约阅读时间需要 18 分钟。

搭建Struts2环境时,我们一般需要做以下几个步骤的工作:

1、找到开发Struts2应用需要使用到的jar文件.
2、创建Web工程
3、在web.xml中加入Struts2 MVC框架启动配置
4、编写Struts2的配置文件

开发Struts2应用依赖的jar文件

大家可以到http://struts.apache.org/download.cgi#struts2014下载struts-2.x.x- all.zip。下载完后解压文件,开发struts2应用需要依赖的jar文件在解压目录的lib文件夹下。不同的应用需要的JAR包是不同的。下面给 出了开发Struts 2程序最少需要的JAR。

struts2-core-2.x.x.jar :Struts 2框架的核心类库

xwork-core-2.x.x.jar :XWork类库,Struts 2在其上构建

ognl-2.6.x.jar :对象图导航语言(Object Graph Navigation Language),struts2框架通过其读写对象的属性

freemarker-2.3.x.jar :Struts 2的UI标签的模板使用FreeMarker编写

commons-logging-1.x.x.jar :ASF出品的日志包,Struts 2框架使用这个日志包来支持Log4J和JDK 1.4+的日志记录。

commons-fileupload-1.2.1.jar :文件上传组件,2.1.6版本后必须加入此文件

以上这些Jar文件拷贝 到Web项目的WEB-INF/lib目录中。

Struts2在web.xml中的启动配置

在struts1.x中, struts框架是通过Servlet启动的。在struts2中,struts框架是通过Filter启动的。在web.xml中的配置如下:

struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*

在StrutsPrepareAndExecuteFilter的init()方法中将会读取类路径下默认的配置文件struts.xml完成初始化操作。

注意:struts2读取到struts.xml的内容后,以javabean形式存放在内存中,以后struts2对用户的每次请求处理将使用内存中的数据,而不是每次都读取struts.xml文件

Struts2应用的配置文件

Struts2默认的配置文件为struts.xml ,该文件需要存放在src目录下,该文件的配置模版如下:

实战--helloworld

Struts2的每个请求需要实现以下内容:
JSP
Action
struts.xml中Action配置

1、新建项目12.01

2、在默认的配置文件struts.xml 中加入如下配置:

/index.jsp
/hello.jsp

在struts2框架中使用包来管理Action,包的作用和java中的类包是非常类似的,它主要用于管理一组业务功能相关的action。在实际应用中,我们应该把一组业务功能相关的Action放在同一个包下。

配置包时必须指定name属性,该name属性值可以任意取名,但必须唯一,他不对应java的类包,如果其他包要继承该包,必须通过该属性进行引 用。包的namespace属性用于定义该包的命名空间,命名空间作为访问该包下Action的路径的一部分,如访问上面例子的Action,访问路径 为:/test/helloworld.action。 namespace属性可以不配置,对本例而言,如果不指定该属性,默认的命名空间为“”(空字符串)。

通常每个包都应该继承struts-default包, 因为Struts2很多核心的功能都是拦截器来实现。如:从请求中把请求参数封装到action、文件上传和数据验证等等都是通过拦截器实现的。 struts-default定义了这些拦截器和Result类型。可以这么说:当包继承了struts-default才能使用struts2提供的核 心功能。 struts-default包是在struts2-core-2.x.x.jar文件中的struts-default.xml中定义。 struts-default.xml也是Struts2默认配置文件。 Struts2每次都会自动加载 struts-default.xml文件。

例子中使用到的com.wuyudong.helloworld.action.HelloWorldAction类如下:

package com.wuyudong.helloworld.action;import com.opensymphony.xwork2.ActionSupport;import com.wuyudong.helloworld.model.MessageStore;public class HelloWorldAction extends ActionSupport {    private static final long serialVersionUID = -4958566543551999157L;    private MessageStore msgStore;    @Override    public String execute() throws Exception {        msgStore = new MessageStore("HelloWorld!");        return SUCCESS;    }    public MessageStore getMsgStore() {        return msgStore;    }    public void setMsgStore(MessageStore msgStore) {        this.msgStore = msgStore;    }}

例子中使用到的hello.jsp如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%>
Hello World!

在struts2中,访问struts2中action的URL路径由两部份组 成:包的命名空间+action的名称,例如访问本例子HelloWorldAction的URL路径为:/helloworld (注意:完整路径为:http://localhost:端口/内容路径/helloworld)。另外我们也可以加上.action后缀访问此 Action。

Action名称的搜索顺序

1.获得请求路径的URI,例如url是:http://server/struts2/path1/path2/path3/test.action

2.首先寻找namespace为/path1/path2/path3的package,如果不存在这个package则执行步骤3;如果存在这 个package,则在这个package中寻找名字为test的action,当在该package下寻找不到action 时就会直接跑到默认namaspace的package里面去寻找action(默认的命名空间为空字符串“” ) ,如果在默认namaspace的package里面还寻找不到该action,页面提示找不到action

3.寻找namespace为/path1/path2的package,如果不存在这个package,则转至步骤4;如果存在这个 package,则在这个package中寻找名字为test的action,当在该package中寻找不到action 时就会直接跑到默认namaspace的package里面去找名字为test的action ,在默认namaspace的package里面还寻找不到该action,页面提示找不到action

4.寻找namespace为/path1的package,如果不存在这个package则执行步骤5;如果存在这个package,则在这个 package中寻找名字为test的action,当在该package中寻找不到action 时就会直接跑到默认namaspace的package里面去找名字为test的action ,在默认namaspace的package里面还寻找不到该action,页面提示找不到action

5.寻找namespace为/的package,如果存在这个package,则在这个package中寻找名字为test的action, 当在package中寻找不到action或者不存在这个package时,都会去默认namaspace的package里面寻找action,如果还 是找不到,页面提示找不到action。

Action配置中的各项默认值

创建 Model类MessageStore

package com.wuyudong.helloworld.model;public class MessageStore {    private String message;    public MessageStore(String msg){        this.setMessage(msg);    }    public String getMessage() {        return message;    }    public void setMessage(String message) {        this.message = message;    }}

创建Action类HelloWorldAction,充当Controller

package com.wuyudong.helloworld.action;import com.opensymphony.xwork2.ActionSupport;import com.wuyudong.helloworld.model.MessageStore;public class HelloWorldAction extends ActionSupport {    private static final long serialVersionUID = -4958566543551999157L;    private MessageStore msgStore;    @Override    public String execute() throws Exception {        msgStore = new MessageStore("HelloWorld!");        return SUCCESS;    }    public MessageStore getMsgStore() {        return msgStore;    }    public void setMsgStore(MessageStore msgStore) {        this.msgStore = msgStore;    }}

配置web.xml

12.01
index.jsp
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*

运行后如图所示

 

转载地址:http://vaaal.baihongyu.com/

你可能感兴趣的文章
AI 从业者该如何选择深度学习开源框架丨硬创公开课
查看>>
OA产品:市场需求推动OA发展
查看>>
史上最大 DDoS 攻击曝光,没想到还有这么多人拿“12345”当密码
查看>>
mysql定时备份
查看>>
《算法设计编程实验:大学程序设计课程与竞赛训练教材》——1.1 机理分析法的实验范例...
查看>>
移动互联网下的服务转型――10086APP成长的探索
查看>>
对呼叫中心外呼业务系统的技术改进
查看>>
视频监控在三大智能领域应用分析
查看>>
微软第三财季净利38亿美元 同比下滑25%
查看>>
三星忘更新域名 数百万设备面临攻击风险
查看>>
部运输服务司“两学一做”关注城市智慧交通系统建设
查看>>
立澜光伏项目提交虚假材料被叫停发电
查看>>
微软SaaS应用拳头产品Dynamics 365售价被泄露
查看>>
中国好DC(数据中心)
查看>>
推动网络流量全面可视化 Gigamon在行动
查看>>
大数据时代继续教育深化发展的机遇与挑战分析
查看>>
黑客“纵横”全球金融系统 中国公司被盯上
查看>>
“光伏贷”推动分布式光伏进入百姓家
查看>>
探究电气设计系统中计算机的应用
查看>>
洛龙区:加快布局大数据产业
查看>>