I'm working on my first java struts2 webapp and want to be able to inject bean parameters into any arbitrary class that is called. But I find that I can only do this with struts action classes...
Let's say I have this bean in my applicationContext.xml file:
<bean id="BeanTest" class="BeanTest">
<property name="test" value="someval" />
</bean>
If I have a struts action class setup called BeanTest (like so), and I add a setter (public void setTest()), then the test parameter will be set and I can access it.
import com.opensymphony.xwork2.ActionSupport;
public class BeanTest extends ActionSupport{
private String test;
public String execute(){
String str = getTest(); // returns "someval"
return "success";
}
public void setTest(String test){
this.test = test;
}
public String getTest(){
return test;
}
}
However, let's say I change the bean to BeanTest2 like so:
<bean id="BeanTest2" class="BeanTest2">
<property name="test" value="someval" />
</bean>
And I have a standalone class like so:
public class BeanTest2{
private test;
public void setTest(String test){
this.test = test;
}
public String getTest(){
return test;
}
}
If I create an instance of BeanTest2 in BeanTest, and call getTest, it always returns null.
import com.opensymphony.xwork2.ActionSupport;
public class BeanTest extends ActionSupport{
public String execute(){
BeanTest2 bt = new BeanTest2();
String str = bt.getTest(); //returns null, but I want "someval"
return "success";
}
}
What I want to do is set up a bean in applicationContext so that I can point it to an arbitrary class and that class will always get whatever bean parameters I set (assuming I've created setters for them). Unfortunately, what happens is that only struts action classes are able to get these bean properties. Everything is not getting set.
Is this question clear? I feel like I'm missing something obvious about the way beans work.