Struts2--Helloworld

搭建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中的配置如下:

<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

Struts2的每个请求需要实现以下内容:

JSP

Action

struts.xml中Action配置

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。

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

<?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>

运行后如图所示

 

时间: 2024-08-02 00:49:48

Struts2--Helloworld的相关文章

struts2 helloworld跳转action时错误500

问题描述 struts2 helloworld跳转action时错误500 是因为这个action跳转不过去吗 500错误空指针 哪里有问题呢 求大神指教 解决方案 贴出错误代码!这样才能更直观,知道是哪里错了! 解决方案二: 拦截后缀是什么!只访问hello没有后缀拦截么! 解决方案三: 楼主的程序中的struts2的filter的url-pattern是啥啊,就是你web.xml中配置的,最好写成/* ,来表示全部路径均拦截,你的struts2的核心配置文件最好放在src下,看着直观些,如果

Java的Struts框架中Action的编写与拦截器的使用方法_java

Struts2 Action/动作动作是Struts2框架的核心,因为他们的任何MVC(模型 - 视图 - 控制器)框架.每个URL将被映射到一个特定的动作,它提供了来自用户的请求提供服务所需的处理逻辑. 但动作也提供其他两个重要的能力.首先,操作从请求数据的传输中起着重要的作用,通过向视图,无论是一个JSP或其它类型的结果.二,动作必须协助的框架,在确定结果应该渲染视图,在响应该请求将被退回. 创建动作:在Struts2的动作,唯一的要求是必须有一个无参数的方法返回String或结果的对象,必

救命!我用ajaxanywhere怎么出现这种错误!!!

问题描述 <!--ajaxAnywhere配置--><filter><filter-name>AjaxAnywhere</filter-name><filter-class>org.ajaxanywhere.AAFilter</filter-class></filter><filter-mapping><filter-name>AjaxAnywhere</filter-name><

Guice整合Struts2简易教程

1.Guice简介 2.Helloworld 简介:本文为如何用Guice进行DI和如何整合Struts2 1. 简介 Guice是Google推出的一款DI框架,因其优秀荣获了Jolt大奖.和Spring相比,要轻量很多.运行时只要指定一个配置类,实现其com.google.inject.Module接口,指定接口和实现类即可. 需要的基础jar包有 Guice需要的jar包,如果是web应用,需要guice-servlet-2.0.jar,整合Struts2则需要guice-struts2-

使用Maven_Jetty构建Struts2工程

1.命令行执行以下语句 mvn archetype:create -DgroupId=com.watson -DartifactId=s2sh -DarchetypeArtifactId=maven- archetype-webapp 就会在当前目录下创建一个myWebapp的web工程 2.我们使用struts2框架,需要添加struts2的依赖到pom.xml里面. 3.我们使用jetty容器来运行我们的web项目,添加jetty的插件到pom.xml里面. pom.xml [html] v

Struts2框架基础篇

首先,要了解Struts2框架中参数传递的大体流程: 服务器端的Web容器收到用户的请求(URL)--Struts2的核心控制器FilterDispatcher接受用户发起的请求,然后判断这个请求是交给action还是交给web组件来处理:如果请求的action或web组件不存在,就会报错404.在整个处理过程中,需要一个辅助对象: ActionMapper ,它会确定调用哪个Action--相应的action会根据struts.xml的配置信息(首先执行拦截此action的所有拦截器,然后再执

Struts2框架学习之三:result返回结果

前言 Struts 2中的Result是作为返回结果的,时当一个action处理完之后返回字符串的结果码.框架可以根据这个返回的字符串,映射到指定的页面.result元素可以分为两部分:一是结果映射,一部分是返回结果类型. 结果映射 result有两个属性可以配置:name属性和type属性.其中的name属性主要用来指定资源的逻辑名称,实际名称在标签内部指定.type属性就是result的返回类型.要注意的是,这两个属性都不是必须的,当我们没有配置这两个属性的时候,实际上框架为我们指定了默认值

struts2服务器端如何提取前端发送过来的json格式字符串value

问题描述 struts.xml中action部分<actionname="test"class="tiku.helloworld"><resulttype="json"></result></action> java中的helloworld类publicclasshelloworldextendsActionSupport{privateStringtype;privateStringuserid;

Struts2框架学习之一:Hello World程序

前言 Struts2框架Apache基金组织下的一个开源框架,基于MVC模式设计的Web应用开发框架.Struts 2是一个用于开发Java EE网络应用程序的开源Web应用框架,它利用并扩展了Java Servlet API,鼓励开发者采用MVC架构.目前struts2的最高版本是2.5(测试版). 快速入门 1.下载和安装 可以从官网直接下载最新的struts开发包,官网有多个版本,使用的时候下载full版本即可.下载之后,只需要进行解压就可以使用了 2.Hello World程序 以下是建

struts2中jsp页面参数不能传递给action

问题描述 struts2中jsp页面参数不能传递给action struts.xml:<?xml version=""1.0"" encoding=""UTF-8""?><!DOCTYPE struts PUBLIC-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.or