struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "James Cook" <James.C...@wecomm.com>
Subject RE: spring, struts2, convention plugin, how to "wire" an action class
Date Thu, 13 May 2010 10:12:43 GMT
Nope, no they are not in the xml for me. I use the @Service/@Repository annotations on the
class, coupled with the component scan in the Spring xml.

-----Original Message-----
From: Jake Vang [mailto:vangjake@googlemail.com] 
Sent: 13 May 2010 11:09
To: Struts Users Mailing List
Subject: Re: spring, struts2, convention plugin, how to "wire" an action class

well, there's something strange about struts2 (with convention plugin)
+ spring. if your action has a field representing a service,
MyInjectedService myInjectedService, you just have to define that in
the spring xml file. for example, your Action class looks like the
following.

public class MyAction extends ActionSupport {
 private Service service;

 @Action(value="/dummy")
 public String dummy() { return SUCCESS; }
 public Service getService() { return service; }
 public void setService(Service service) { this.service = service; }
}

in your spring xml file (i.e. applicationContext.xml), you simply
define a bean with the id="service". for example,

<bean id="service" class="my.class.Service"/>

that's it; you don't have to do anything else. you don't even have to
explicitly say (using XML) to inject this when you are creating an
instance of MyAction. now when a user accesses /dummy, MyAction will
be created and its service field will actually be injected with what
is specified in the spring xml file.

what irks me or  bothers me is that it is not obvious at the moment
how to simply inject strings or booleans into the Action class. it
should be just as simple.

upon analyzing what i am doing, and in light of what you said, perhaps
i am trying to push some logic into the Action class that shouldn't be
there. perhaps i should push the logic to a service class instead. in
this case, this problem goes away. BUT, the question remains, how can
i use DI with struts2 (convention plugin)  and spring on Action
classes? or can i not? if i can't, end of story. if i can, how?

On Thu, May 13, 2010 at 5:44 AM, James Cook <James.Cook@wecomm.com> wrote:
> Hmm. I use the same combo.
>
> I found at some point I could jsut do
>
> public class MyAction extends ActionSupport {
>
> private MyInjectedService service
>
> etc etc
>
> But I have started doing:
>
> public class MyAction extends ActionSupport {
>
> @Autowired
> private MyInjectedService service
>
> I am not sure if that alters the way Struts2/Spring does it. But it does
> make it a bit clearer to read.
>
> -----Original Message-----
> From: Jake Vang [mailto:vangjake@googlemail.com]
> Sent: 13 May 2010 10:23
> To: user@struts.apache.org
> Subject: spring, struts2, convention plugin, how to "wire" an action
> class
>
> i am using struts 2.1.8.1 and the convention plugin. i am also using
> spring for dependency injections (DI). my question is if it is
> possible to to use struts2 + convention plugin with spring for DI on
> my Action classes? i have searched  the internet but only seen
> examples using struts 1 + spring for DI on Action classes. the
> convention plugin makes life simpler (no XML), but also, partially
> because of little documentation, makes it uneasy to do certain things.
> i wonder if this is part of the reason why DI on Action classes using
> spring + struts is not obvious for me.
>
> Here's a simple code. This is my action class.
>
> public class SpringWiringAction extends ActionSupport {
>  private String message = "no dependency injection";
>
>  @Action(value="/spring-wiring")
>  public String springWiring() {
>  return SUCCESS;
>  }
>
>  public String getMessage() { return message; }
>  public void setMessage(String message) { this.message = message; }
> }
>
> My "view" or the JSP page corresponding to the Action is:
> /webapp/WEB-INF/content/spring-wiring-success.jsp.
>
> <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
> pageEncoding="ISO-8859-1"%>
> <%@taglib uri="/struts-tags" prefix="s" %>
> <html>
>  <head><title>Test Spring Wiring</title</head>
>  <body>
>  <s:property value="message"/>
>  </body>
> </html>
>
> My web.xml is setup according to
> http://struts.apache.org/2.0.14/docs/struts-2-spring-2-jpa-ajax.html.
>
> My spring XML file (/webapp/WEB-INF/applicationContext.xml) is defined
> as following.
>
> ...
> <bean id="placeholderConfig"
> class="org.springframework.beans.factory.config.PropertyPlaceholderConfi
> gurer">
>  <property name="location"
> value="WEB-INF/applicationContext.properties"/>
> </bean>
> ...
> <bean name="/spring-wiring" class="com.me.actions.SpringWiringAction">
>  <property name="message" value="${message}"/>
> </bean>
>
> My /webapp/WEB-INF/applicationContext.properties file then has this
> content.
>
> message=dependency inject success
>
> when i start up tomcat 6, everything starts up correctly and there are
> no complaints. however, when i go to
> http://localhost/webapp/spring-wiring, the message that gets displayed
> is "no dependency injection".
>
> is there something that i am missing using spring + struts2 (with the
> convention plugin) to use DI on Actions?
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message