struts2.0入门级例子

struts2.0入门级例子

包依赖基础包:
1. commons-logging-1.1.jar
2. freemarker-2.3.8.jar
3. ognl-2.6.11.jar
4. struts2-core-2.0.8.jar
5. xwork-2.0.3.jar
入门级例子

1、首先拷贝需要的基础依赖包(去除重复的jar包,保留高版本的)
2
、在web.xml中,配置FilterDispatcher过滤器
     <filter>
         <filter-name>struts2</filter-name>
         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
     </filter>

     <filter-mapping>
         <filter-name>struts2</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>
3
、编写一个简单的Action类,这个类简单到不能再简单了!
public class VerySimpleAction {
       public String hello(){
               return "success";
       }
}
struts2中,Action类不必一定要继承一个父类(虽然也可以这样做,你可以继承ActionSupport类)。当然,你需要返回一个String类型的值,这个值代表的意思是,你的下一步要到哪里去。

4
、在类路径下,添加struts.xml配置文件,这个配置文件,是对Action的配置:
<?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>
     <package name="simple" namespace="/simple" extends="struts-default">
         <action name="hello"
                 class="com.bjsxt.crm.web.struts2.test.VerySimpleAction"
                 method="hello"
         >
             <result name="success">/struts2/test/index.jsp</result>
         </action>
     </package>
</struts>

5
、在相应位置提供JSP文件
<%@ page language="java" contentType="text/html; charset=GB18030"
     pageEncoding="GB18030"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>
世界你好</title>
</head>
<body>
世界你好

</body>
</html>

6
、打开IE浏览器,在地址栏输入:
http://localhost:[
端口]/[Context Path]/simple/hello.action
至此,第一个struts2程序完成了。下面,是关于这个程序的问题的解答,以及进一步的讨论:


为什么需要输入.action结尾的URL地址?而不是.do
Struts2
在默认情况下,会将.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.org/dtds/struts-2.0.dtd">

<struts>
     <constant name="struts.action.extension" value="action,do,webwork" />

     <package name="simple" namespace="/simple" extends="struts-default">
         <action name="hello" 
                 class="com.bjsxt.crm.web.struts2.test.VerySimpleAction"
                 method="hello"
         >
             <result name="success">/struts2/test/index.jsp</result>
         </action>
     </package>
</struts>

这 将使得struts2,对于所有以.action.do.webwork扩展名结尾的请求均进行处理!当然,它也可以在web.xml中或其它地方进 行配置,struts2提供了非常灵活的方式,请【参考相应的文档(docs/constant-configuration.html)】或参考 struts2-core-2.0.8.jar/org/apache/struts2下面的default.properties文件(这个文件在 struts的核心jar包中)。

Struts2
都有哪些配置文件?Struts.xml是主要的配置文件,当然通过include标签可以将它拆分。比如,在struts.xml加入:
<include file="example.xml"/>
即可添加一个example.xml文件,这个文件的配置与struts.xml文件的配置一致。往往大型应用中会按模块来划分配置文件。

另外还可以配置一些配置文件,请【参考docs/configuration-files.html


通过使用annotationJDK5的新特性),也可以完全取消这些配置文件。达到零配置文件的目的。也请参考相关的文档说明。

Struts2
配置【请参考:docs/configuration-elements.html

相关概念:

Package
:用来组??Action及其一系列配置的方式
Namespace:
页面访问的起始路径
Action
:处理请求
Result:
结果转向
拦截器:将要?褂媚男菇仄?
Include:
可以将文件进行拆分,即通过include标签将一个配置文件包含进另外一个配置文件中


Struts2
Action类?Struts2ACTION类可以是普通的POJO对象。方法只要返回字符串类型即可。这些字符串,正代表了下一步的转向。这些Action类,每次请求都会创建一个新的对象。

Struts2
Action返回参数(result)?用一个字符串代表result,然后可以定义一个视图(比如某个JSP页面),与之匹配。

定义在<action/>标签内部的result是局部result,也可以定义全局result

        <global-results>
            <result name="login" type="redirect">/Login.action</result>
            <result name="Exception">/Exception.jsp</result>
        </global-results>

默认的resultJSP视图,我们可以通过指定result的类型来指定其使用不同的视图技术,如:
<result type="freemarker">/struts2/test/freemarker.ftl</result>

另外还有各种result 类型。

【请参考:docs/result-types.html

如何从浏览器向ACTION传递参数?在Action类中定义属性,并提供getters/setters方法即可。

如何将参数从action传递到JSP
JSP中,使用:

<%@ taglib prefix="s" uri="/struts-tags" %>
引入struts2taglib
调用:<s:property value="strvalue" />这样的标签,可以把值,从Action的属性中取出。


或者使用ActionContext.getContext().put()方法来传值。
或通过ServletActionContext.getRequest()/getSession()等方法往request/session中传值,在页面上可以通过:
<s:property value="#session.mySessionPropKey"/> or
<s:property value="#session["mySessionPropKey"]"/> or
<s:property value="#request["myRequestPropKey"]/>

这样的方式来取值,【请参考:docs/ognl.html

传递参数的内部机制是什么?(ValueStack)
概述当请求到达ActionProxy进行处理之前,它会将界面传递过来的参数,经过转换之后,设置到Action对象中。 这种转换是通过OGNL进行的。


OGNL
Object Graph Navigation Language)是一种强大的对象导航语言,通过OGNL可以用一种非常简易的方式来给对象设值或从对象取值。而Struts2正是依赖于OGNL的这 种设值和取值机制,来将页面数据转换为相应的类型并设置到Action对象中;或者从页面中读取数据的。

Struts2
OgnlValueStack,正是对Ognl的封装。ValueStack中会包含有当前环境中的各种对象信息。我们也可以通过调用ActionContext.getContext().put()方法,来往这个ValueStack中添加对象。

Struts2
会将这个ValueStack放入request scope中,并取值为:struts.valueStack

OGNLl        

context
context
是一个Map结构,可以自定义变量,并在ognl中的表达式中引用。使用#开头来引用这些变量

root
根对象,对于根对象,无需使用#开头来访问其属性值

根据实际例子来讲解OGNL的使用

ValueStack

Spring如何集成?
1
、将struts2-spring-plugin-2.0.8.jar加入类路径

2
、在配置文件中,添加属性:
<constant name="struts.objectFactory" value="spring"/>
3
、即可以在spring中定义Action类,同时把struts2的配置文件中的class属性改为spring配置中的id值即可

如何处理异常?通过类似如下配置可以定义出现一个异常的时候,应该转向什么页面来呈现:
     <global-results>
         <result name="SystemException">/struts2/test/exception.jsp</result>
     </global-results>
     <global-exception-mappings>
         <exception-mapping result="SystemException"
         exception="com.bjsxt.crm.manager.SystemException"></exception-mapping>
     </global-exception-mappings>

当出现SystemException异常之后,? ?exception.jsp页面,在exception.jsp页面上,可以这样写:
这是异常信息页面!【<s:property value="exception.message"/>
从上面简单的例子知道,exception对象本身被添加到了ValueStack

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值