`
sha1064616837
  • 浏览: 39910 次
  • 性别: Icon_minigender_2
  • 来自: 湖南
社区版块
存档分类
最新评论

struts2 annotation

阅读更多
声明:我spring也是用的annotation注释的
1.引入依赖的jar包:struts2-convention-plugin.jar
2.在application.xml中加上配置,使得struts与spring的annotation互通
application.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <import resource="classpath*:/platform.xml" />
	<import resource="sodn-server.xml" />
	<context:component-scan base package="xxx.web.action"/>
	<tx:annotation-driven />
</beans>

3.我创建的一个package 继承struts-default,自定义了用户登陆拦截器,和两个全局result。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>
	<package name="default" extends="struts-default"> 
        <!-- 定义一个拦截器  拦截用户必须先登录-->  
        <interceptors>  
            <interceptor name="authority" class="xxx.filter.LoginInterceptor" />  
            <!-- 拦截器栈 -->  
           <interceptor-stack name="mydefault">  
              	<interceptor-ref name="defaultStack" />  
				<interceptor-ref name="authority" />  
            </interceptor-stack>  
        </interceptors>  
  
        <!-- 定义全局Result -->  
        <global-results>  
           <!-- 当返回login视图名时,转入/login.jsp页面 -->  
           <!--<result name="login" >user/login.action</result>  --> 
            <result name="login" >/jsp/login.jsp</result>
            <result name="input" >/jsp/login.jsp</result>
        </global-results>  
	
	</package>	
	<constant name="struts.i18n.encoding" value="UTF-8" />
</struts>

4.常用的struts2 annotation
//声明父包
@ParentPackage("default")
//声明拦截器,多个之间用,隔开
@InterceptorRefs({ 
	@InterceptorRef("mydefault")
})
//声明命名空间,默认为/ 
@Namespace(value="/business")
//声明结果集的路径
@ResultPath("/jsp")
//一个方法对应多个action(一般用于页面跳转)
@Actions({
	@Action(value = "goToOrder", 
		results = { 
			@Result(name = "success", location = "/jsp/business/order.jsp")
		}) ,
	@Action(value = "goToOptical",
		results={
			@Result(name = "success", location = "/jsp/business/optical.jsp")
		}),
	@Action(value = "goToInspect",
		results={
			@Result(name = "success", location = "/jsp/business/inspect.jsp")
		})
})
//单个方法的action,没有返回值
@Action(value = "saveOrder")
分享到:
评论

相关推荐

    在嵌入式jetty环境下运行struts2Annotation项目

    在嵌入式Jetty环境下运行Struts2 Annotation项目是一个常见的任务,特别是在开发和测试阶段,因为这种方式能够快速启动服务,而无需依赖大型服务器容器。本文将深入探讨如何配置和执行这个过程,以及涉及的关键技术...

    struts2annotation json

    标题“struts2annotation json”暗示我们将探讨如何在Struts2中使用注解来处理JSON相关的功能。首先,让我们深入理解Struts2的注解系统。 1. **Struts2注解**: - `@Action`: 这个注解用于标记一个方法为处理HTTP...

    struts2 annotation 批量下载

    在Struts2框架中,使用注解(Annotation)可以极大地简化控制器类的配置,提高代码的可读性和维护性。本文将深入探讨如何利用Struts2的注解功能实现批量下载功能,并通过创建临时文件来处理下载请求,同时确保在下载...

    struts2 使用Annotation 配置的小例子

    在这个小例子中,我们将深入探讨如何利用Struts2的Annotation配置来构建一个简单的应用。 首先,让我们了解什么是Annotation。在Java中,Annotation是一种元数据,它提供了一种安全的方式向编译器、JVM或者第三方...

    struts2 annotation 文件下载

    import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.Result; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.spring...

    struts2 Annotation 版本学习心得与例子

    Struts2的“零配置”特性是Struts2的新功能,可能会出现一些小Bug,所以企业开发者请慎重使用该特性, ...如果用的是Annotation的Struts2,就要将struts.xml去掉,否则即使将struts.xml中的内容注销,也会报错;

    Struts2之Annotation注解配置使用案例struts013

    在Struts2中,Annotation注解的引入为开发者提供了更加灵活和便捷的配置方式,使得无需在XML配置文件中进行繁琐的设置,可以直接在类或方法上通过注解来进行配置。本文将深入探讨Struts2中的Annotation配置,以及...

    struts2 hibernate3 spring2.5 annotation 整合

    Struts2、Hibernate3和Spring2.5是Java Web开发中的三大框架,它们各自负责不同的职责,但可以协同工作以构建高效的企业级应用。这里主要讨论的是如何将这三者结合,并利用注解(Annotation)进行配置,以简化开发...

    struts2-Annotation

    在给定的“struts2-Annotation”主题中,重点是Struts2框架如何利用注解(Annotation)来增强其功能和简化配置。注解是一种元数据,可以在代码中嵌入,提供有关类、方法或字段的额外信息,而无需编写XML配置文件。 ...

    使用struts2的annotation验证

    博文链接:https://flym.iteye.com/blog/174358

    Struts2使用Annotation返回Json

    在Struts2中,使用注解(Annotation)可以简化配置,提高开发效率。本篇文章将深入探讨如何在Struts2中通过注解实现返回JSON数据的功能。 首先,让我们理解JSON(JavaScript Object Notation)是一种轻量级的数据...

    struts2 annotaion

    Struts2 Annotation是Struts2框架的一个特性,它允许开发者使用Java注解的方式来替代XML配置文件,简化Action类与URL的映射关系。这个特性主要由`struts2-convention-plugin`插件提供支持,因此在使用前需要将对应的...

    ssh2 Annotation注解 框架+oracle

    **Struts2 Annotation注解** Struts2框架同样支持注解,如: - `@Action`注解标识一个方法作为执行的动作,替代了传统的struts.xml配置。 - `@Results`和`@Result`用于定义动作的结果页面或跳转逻辑。 - `@Params`...

    struts annotation Hello World

    通过这个"struts annotation Hello World"的学习,你可以掌握如何在Struts 2中使用注解来简化开发流程,同时也能对MVC架构有一个基本的理解。进一步研究Struts 2的其他注解和特性,将有助于构建更高效、更易于维护的...

    struts2 interceptor annotation plugin

    而"struts2 interceptor annotation plugin"则是Struts2框架提供的一种使用注解来配置拦截器的方式,这种方式更加简洁、直观,减少了XML配置文件的复杂性。 注解(Annotation)是Java编程语言的一个重要特性,它...

    struts2利用注解annotation实现文件下载

    ### Struts2 使用注解(Annotation)实现文件下载 在Web开发中,文件上传与下载是常见的需求之一。Struts2框架提供了强大的功能来支持这一需求。本文将详细介绍如何使用Struts2框架结合注解(Annotation)的方式...

    ssh的annotation示例

    **Struts2 Annotation** 在Struts2框架中,注解可以用于控制器(Actions)的定义。例如,`@Action`注解用来标记一个类或方法为Struts2的Action。它可以包含`value`属性来指定URL映射,`results`属性来定义不同的...

    struts annotation.ppt

    Struts2注解是Java开发框架Struts2中的一种特性,它引入了JDK1.5及更高版本的注解(Annotation)概念,使得开发者能够更简洁地配置Struts2框架,减少XML配置文件的使用,提高开发效率。注解提供了一种方式,将元数据...

Global site tag (gtag.js) - Google Analytics