gpt4 book ai didi

java - 如何使用注释将 struts2 Action 的实例化委托(delegate)给 spring

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:26:57 25 4
gpt4 key购买 nike

您好,我有一个 Struts2 应用程序,其中在 struts.xml 文件中定义了 struts 操作。现在,我想将其委托(delegate)给 Spring,以便在 session 范围内执行操作。

  • 案例 1:如果我使用注释 @Repository("myAction"),我总是会收到错误消息:无法实例化 ActionmyAction,在命名空间 /omb doAction

  • 中为 doAction 定义
  • 情况 2:如果我使用 applicationContext.xml,我在 myService.callMethode(); 上有一个 NullPointerException/p>

如何使用注释来做到这一点?因为重新定义applicationContext.xml

中的所有服务会太长

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.devMode" value="false" />
<constant name="struts.action.extension" value="do" />
<constant name="struts.custom.i18n.resources" value="com.omb.i18n.StrutsResourceBundle" />
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<constant name="struts.objectFactory.spring.autoWire" value="name" />
<constant name="struts.i18n.encoding" value="ISO-8859-1" />
<constant name="struts.i18n.reload" value="false" />
<constant name="struts.configuration.xml.reload" value="false" />
<constant name="struts.locale" value="fr" />
<constant name="struts.multipart.maxSize" value="100000000000" />
<constant name="struts.enable.SlashesInActionNames" value="true" />
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>

<constant name="struts.codebehind.classSuffix" value="Controller"/>
<constant name="struts.codebehind.action.checkImplementsAction" value="false"/>
<constant name="struts.codebehind.action.checkAnnotation" value="false"/>
<constant name="struts.codebehind.action.defaultMethodName" value="index"/>
<constant name="struts.configuration.classpath.defaultParentPackage" value="rest-default" />

<package name="default" extends="tiles-default" namespace="/">

<interceptors>

<interceptor name="params-filter"
class="com.opensymphony.xwork2.interceptor.ParameterFilterInterceptor" />

<interceptor-stack name="defaultStack">
<interceptor-ref name="exception" />
<interceptor-ref name="alias" />
<interceptor-ref name="servletConfig" />
<interceptor-ref name="i18n" />
<interceptor-ref name="chain" />
<interceptor-ref name="modelDriven" />
<interceptor-ref name="fileUpload">
<param name="maximumSize">11204928</param>
</interceptor-ref>
<interceptor-ref name="staticParams" />
<interceptor-ref name="conversionError" />
<interceptor-ref name="params" />
<interceptor-ref name="prepare" />
<interceptor-ref name="validation" />
<interceptor-ref name="workflow" />
</interceptor-stack>

</interceptors>

<default-interceptor-ref name="defaultStack" />

<global-results>
<result name="technicalError" type="chain">
errorAction
</result>
<result name="sessionInvalidError" type="tiles">
sessionInvalid
</result>
<result name="blank" type="tiles">blank</result>
</global-results>

<global-exception-mappings>
<exception-mapping exception="java.lang.Exception"
result="technicalError" />
<exception-mapping
exception="com.omb.service.exception.UserSessionInvalidException"
result="sessionInvalidError" />

</global-exception-mappings>

</package>

<package name="omb" extends="default" namespace="/omb">
<action name="doAction.do" class="myAction" method="{1}">
<result name="success" type="redirectAction">
<param name="namespace">/omb</param>
<param name="actionName">displayResult</param>
</result>
<result name="error" type="redirectAction">
<param name="namespace">/error</param>
<param name="actionName">displayError</param>
</result>
</action>
</package>
</struts>

web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<display-name>MyApplication</display-name>
<description>Application OMB</description>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml,
classpath:applicationContext-web.xml
</param-value>
</context-param>

<context-param>
<param-name>org.apache.tiles.DEFINITIONS_CONFIG</param-name>
<param-value>/WEB-INF/tiles/tiles-defs.xml</param-value>
</context-param>

<context-param>
<param-name>struts.ajax.debug</param-name>
<param-value>${struts.ajax.debug}</param-value>
</context-param>

<filter>
<filter-name>Acegi Filter Chain Proxy</filter-name>
<filter-class>
org.acegisecurity.util.FilterToBeanProxy
</filter-class>
<init-param>
<param-name>targetClass</param-name>
<param-value>
org.acegisecurity.util.FilterChainProxy
</param-value>
</init-param>
</filter>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>Acegi Filter Chain Proxy</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- Java Melody filter mapping -->
<filter-mapping>
<filter-name>monitoring</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
<listener-class>
com.omb.listener.ApplicationListener
</listener-class>
</listener>

<listener>
<listener-class>
org.apache.struts2.tiles.StrutsTilesListener
</listener-class>
</listener>

<!-- listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener-->
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

MyAction.java:

package com.omb.actions;

@Repository("myAction")
@Scope("session")
public class MyAction extends ActionSupport {

@Qualifier("myService")
private MyService myService;

public String execute() throws Exception {
myService.callMethode();
return SUCCESS;
}


public MyService getMyService() {
return this.myService;
}

public void setMyService(MyService myService) {
this.myService = myService;
}
}

最佳答案

如果你使用的是 Spring 插件,那么 struts 与 spring 的集成就成功了应用上下文。但是action bean是由struts容器管理的,所以它内部保留了DI的可能性。如果您想完全委托(delegate)由 spring 管理的 bean,您应该在应用程序上下文中配置操作 bean,并在操作配置中替换对 spring id 的类引用。

applicationContext.xml的地方

<!--<context:annotation-config/>-->
<context:component-scan base-package="your.base.package"/>

指向 spring 扫描包层次结构中的 bean。

关于java - 如何使用注释将 struts2 Action 的实例化委托(delegate)给 spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20002779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com