Struts2--Helloworld
搭建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中的配置如下:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在StrutsPrepareAndExecuteFilter的init()方法中将会读取类路径下默认的配置文件struts.xml完成初始化操作。
注意:struts2读取到struts.xml的内容后,以javabean形式存放在内存中,以后struts2对用户的每次请求处理将使用内存中的数据,而不是每次都读取struts.xml文件
Struts2应用的配置文件
Struts2默认的配置文件为struts.xml ,该文件需要存放在src目录下,该文件的配置模版如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts> </struts>
实战--helloworld
1、新建项目12.01
2、在默认的配置文件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>
<constant name="struts.devMode" value="true" />
<package name="Hello_World_Struts2" extends="struts-default">
<action name="index">
<result>/index.jsp</result>
</action>
<action name="hello" class="com.wuyudong.helloworld.action.HelloWorldAction" method="execute">
<result name="success">/hello.jsp</result>
</action>
</package>
</struts>
在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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World!</title>
</head>
<body>
<h2>
<s:property value="msgStore.message" />
</h2>
</body>
</html>
在struts2中,访问struts2中action的URL路径由两部份组成:包的命名空间+action的名称,例如访问本例子HelloWorldAction的URL路径为:/helloworld (注意:完整路径为:http://localhost:端口/内容路径/helloworld)。另外我们也可以加上.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
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
<?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>12.01</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
运行后如图所示
Struts2--Helloworld的更多相关文章
- 菜鸟学Struts2——HelloWorld
写在前面 自从工作后就过上了只有一个月记忆的生活,太健忘,很多学过的东西因为用得少便忘记了,第二次学习struts,为了以后便于查阅,开始自己的博客之旅.Struts的学习还是从Hello World ...
- struts2学习(1)struts2 helloWorld
一.struts2简介: 二.helloWorld: 1)工程结构: HelloWorldAction.java: package com.cy.action; import com.opensymp ...
- Intellij Idea + Maven + Git + Struts2 HelloWorld
1.在intellij Idea上新建Maven项目,输入相应的groupId,artifactId,项目名称: 2.在项目的pom文件中,引入struts2的核心依赖struts2-core: &l ...
- Struts2之HelloWorld
首先既然是开发Struts程序的话,那么自然需要用到Struts2开发包,Struts2是apache旗下的开源框架,所有的开发包和源代码都可以在Apache官网下载. 那么,就来开始编写第一个Str ...
- 2. Struts2 基础
1. Struts2简介 Struts2是一个WEB端MVC框架.作为比较早的MVC 框架之一,Struts2在使用中还是比较多的.虽然个人感受没有SpringMVC还那么的好用 Struts2 官网 ...
- 请求参数到表述层的类型转换——Struts2
一.简介 说明:HTTP 协议传输数据没有类型的概念,在服务器端是通过 request.getParameter().request.getParameterValue() 方法得到请求参数为 Str ...
- struts2基础——标签
一.通用标签 1.s:property (读取值栈中对象的属性值) 属性:value:指定OGNL表达式:default:OGNL表达式返回为 null 时,使用默认值:escape:是否对 HTML ...
- struts2基础——请求与响应、获取web资源
一.请求与响应 Action1.含义:(1) struts.xml 中的 action 元素,也指 from 表单的 action 属性,总之代表一个 struts2 请求.(2) 用于处理 Stru ...
- struts2基础——最简单的一个例子
学习版本:struts-2.3.15.3 一.导入jar包,可以参考 官方项目 blank. 二.添加配置文件:web.xml struts.xml web.xml: <filter> & ...
- Hello Struts2
Struts2 概述 Struts2 是一个用来开发 MVC 应用程序的框架. 它提供了 Web 应用程序开发过程中的一些常见问题的解决方案: 对来自用户的输入数据进行合法性验证; 统一的布局; 可扩 ...
随机推荐
- Android View的加载过程
大家都知道Android中加载view是从Activity的onCreate方法调用setContentView开始的,那么View的具体加载过程又是怎么的呢?这一节我们做一下分析. 首先追踪一下代码 ...
- LuaInterface简介
Lua是一种很好的扩展性语言,Lua解释器被设计成一个很容易嵌入到宿主程序的库.LuaInterface则用于实现Lua和CLR的混合编程. (一)Lua from the CLR 测试环境:在VS2 ...
- Unity 全面理解加载和内存管理
最近一直在和这些内容纠缠,把心得和大家共享一下: Unity里有两种动态加载机制:一是Resources.Load,一是通过AssetBundle,其实两者本质上我理解没有什么区别.Resources ...
- linux 硬盘分区,分区,删除分区,格式化,挂载,卸载笔记
linux 虽然一直都有在玩,但是对硬盘操作确实不是很熟悉今天有空,就整理了下. 1, 创建分区 先查看下是否有磁盘没有分区 fdisk -l 其中第一个框和第二个框,是已经分好区的磁盘,第三个硬盘没 ...
- codeforces MUH and Cube Walls
题意:给定两个序列a ,b, 如果在a中存在一段连续的序列使得 a[i]-b[0]==k, a[i+1]-b[1]==k.... a[i+n-1]-b[n-1]==k 就说b串在a串中出现过!最后输出 ...
- Hadoop第5周练习—MapReduce计算气象温度等例子
:对云计算的看法 内容 :使用MapReduce求每年最低温度 内容 :求温度平均值能使用combiner吗? 内容 :使用Hadoop流求最高温度(awk脚本) 内容 :使用Hadoop流求最高温度 ...
- Angular系列------AngularJS入门教程:导言和准备(转载)
学习AngularJS的一个好方法是逐步完成本教程,它将引导您构建一个完整的AngularJS web应用程序. 该web应用是一个Android设备清单的目录列表,您可以筛选列表以便查看您感兴趣的设 ...
- Angular系列------AngularJS快速开始(转载)
Hello World! 开始学习AngularJS的一个好方法是创建经典应用程序“Hello World!”: 使用您喜爱的文本编辑器,创建一个HTML文件,例如:helloworld.html. ...
- 面向对象的JavaScript(3):私有成员和公开成员
在小项目中对于JavaScript使用,只要写几个function就行了.但在大型项目中,尤其是在开发追求 良好的用户体验的网站中,如SNS,就会 用到大量的JavaScrpt,有时JavaScrip ...
- 第一个app.总结
前记: 最近想整点外快,但是又没啥子技术,唉,学了一下android,想写点游戏啥的,,唉,可惜,美工,UI始终不行,代码也勉勉强强... 不过总的来说也是收获参半吧,也是有一些新的知识学到了嘛,至少 ...