annotations in Spring MVC - java

I'd like to convert this SimpleFormController to use the annotation support introduced in Spring MVC 2.5
Java
public class PriceIncreaseFormController extends SimpleFormController {
ProductManager productManager = new ProductManager();
#Override
public ModelAndView onSubmit(Object command)
throws ServletException {
int increase = ((PriceIncrease) command).getPercentage();
productManager.increasePrice(increase);
return new ModelAndView(new RedirectView(getSuccessView()));
}
#Override
protected Object formBackingObject(HttpServletRequest request)
throws ServletException {
PriceIncrease priceIncrease = new PriceIncrease();
priceIncrease.setPercentage(20);
return priceIncrease;
}
}
Spring Config
<!-- Include basic annotation support -->
<context:annotation-config/>
<!-- Comma-separated list of packages to search for annotated controllers. Append '.*' to search all sub-packages -->
<context:component-scan base-package="springapp.web"/>
<!-- Enables use of annotations on controller methods to map URLs to methods and request params to method arguments -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean name="/priceincrease.htm" class="springapp.web.PriceIncreaseFormController">
<property name="sessionForm" value="true"/>
<property name="commandName" value="priceIncrease"/>
<property name="commandClass" value="springapp.service.PriceIncrease"/>
<property name="validator">
<bean class="springapp.service.PriceIncreaseValidator"/>
</property>
<property name="formView" value="priceincrease"/>
<property name="successView" value="hello.htm"/>
<property name="productManager" ref="productManager"/>
</bean>
Basically, I'd like to replace all the XML configuration for the /priceincrease.htm bean with annotations within the Java class. Is this possible, and if so, what are the corresponding annotations that I should use?
Thanks,
Don

It'll look something like the following, although whether it works or not exactly as is will depend a bit on your configuration (view resolver, etc). I should also note that there are about eight billion valid ways to write this thing. See the Spring documentation, 13.11.4 "Supported handler method arguments and return types" for an overview of the insanity. Also note that you can autowire the validator
#Controller
#RequestMapping("/priceincrease.htm")
public class PriceIncreaseFormController {
ProductManager productManager;
#Autowired
public PriceIncreaseFormController(ProductManager productManager) {
this.productManager = productManager;
}
// note: this method does not have to be called onSubmit
#RequestMapping(method = RequestMethod.POST)
public String onSubmit(#ModelAttribute("priceIncrease") PriceIncrease priceIncrease, BindingResult result, SessionStatus status {
new PriceIncreaseValidator().validate(priceIncrease, result);
if (result.hasErrors()) {
return "priceincrease";
}
else {
int increase = priceIncrease.getPercentage();
productManager.increasePrice(increase);
status.setComplete();
return "redirect:hello.htm";
}
}
// note: this method does not have to be called setupForm
#RequestMapping(method = RequestMethod.GET)
public String setupForm(Model model) {
PriceIncrease priceIncrease = new PriceIncrease();
priceIncrease.setPercentage(20);
model.addAttribute("priceIncrease", priceIncrease);
return "priceincrease";
}
}

Someone completed this project with a recent MVC and it's on github, so you can see how all the classes are changed compared to Spring's tutorial.
Link: PriceIncreaseFormController

Related

How do I perform Constructor-based dependency injection with Spring using annotations?

OK, so if I need to put some primitive values in the constructor, how do I do that?
#Autowired
public CustomBean(String name, #Qualifier("SuperBean") SuperBean superBean) {
super();
this.superBean = superBean;
this.name = name;
}
For instance here I am defining that the superBean has the Qualifier "SuperBean", but I'd also like to know how is it possible to use annotations to set the name value here?
I know it's possible with xml configuration, but I want to know how to do this with annotations too:
<bean id="CustomXmlBean" class="org.arturas.summerfav.beans.CustomXmlBean">
<constructor-arg name="name" type="String" value="The Big Custom XML Bean" />
<constructor-arg>
<bean id="SuperBean" class="org.arturas.summerfav.beans.SuperBean" />
</constructor-arg>
</bean>
Well how do I put in values for String, int and other generic types?
Here is one way to do this:
#Component
public class YourBean {
#Autowired
public YourBean(#Value("${prop1}") String arg1, #Value("${prop2}") String arg2) {
// rest of the code
}
}

Spring Data web pagination "page" parameter not working

I'm trying to get Spring Data's web pagination working. It's described here:
http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/repositories.html#web-pagination
Here's my Java (Spring Web MVC #Controller handler method):
#RequestMapping(value = "/list", method = RequestMethod.GET)
public String list(
#PageableDefaults(value = 50, pageNumber = 0) Pageable pageable,
Model model) {
log.debug("Params: pageNumber={}, pageSize={}",
pageable.getPageNumber(), pageable.getPageSize());
...
}
And here's my Spring configuration:
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class="org.springframework.data.web.PageableArgumentResolver" />
</mvc:argument-resolvers>
</mvc:annotation-driven>
(It appears that the configuration above is the way to do this now; the configuration approach described in the link is deprecated.)
When I actually try to control the pagination using the page and page.size parameters, the latter works just fine, but the former doesn't. For example, if I hit
http://localhost:8080/myapp/list?page=14&page.size=42
the log output is
Params: pageNumber=0, pageSize=42
So I know that the argument resolver is kicking in, but not sure why it's not resolving the page number. I've tried a bunch of other param names (e.g. page.number, pageNumber, page.num, etc.) and none of them work.
Is this working for anybody else?
the page parameter is actually a bit non-intuitive - page.page and not page, changing to page.page should get things to work.
Looking into the PageableArgumentResolver I found that prefix and separator are configurable, so you can configure the class not to have it.
public class PageableArgumentResolver implements WebArgumentResolver {
private static final Pageable DEFAULT_PAGE_REQUEST = new PageRequest(0, 10);
private static final String DEFAULT_PREFIX = "page";
private static final String DEFAULT_SEPARATOR = ".";
in my #Configuration class I did something different to achieve using page, size,sort and sortDir as default.
#Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
PageableArgumentResolver resolver = new PageableArgumentResolver();
resolver.setPrefix("");
resolver.setSeparator("");
argumentResolvers.add(new ServletWebArgumentResolverAdapter(resolver));
}
Now this works
http://:8080/myapp/list?page=14&size=42
You can override parameters if you want by doing:
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class="org.springframework.data.web.PageableHandlerMethodArgumentResolver">
<property name="oneIndexedParameters" value="true"></property>
<property name="pageParameterName" value="page"></property>
<property name="sizeParameterName" value="size"></property>
</bean>
</mvc:argument-resolvers>
</mvc:annotation-driven>

How to conditionally initialization a class via spring

How to conditionally initialization a class via spring?
If some condtion is true then i want one argument to be passed else some other
argument
<bean id="myFactory" class="Factory">
if something then
<constructor-arg>
<util:map>
<!-- configure your map here, or reference it as a separate bean -->
<entry key="java.lang.String" value="key">....</entry>
</util:map>
</constructor-arg>
else
<constructor-arg>
<util:map>
<!-- configure your map here, or reference it as a separate bean -->
<entry key="java.lang.String" value="key">....</entry>
</util:map>
</constructor-arg>
</bean>
How?
Spring Expression Language might do the trick for you. link
You can do it exactly the way that you have specified. Define a FactoryBean this way, say for eg. For generating a Customer Bean:
public class CustomFactoryBean implements FactoryBean<Customer>{
private int customProperty;
public int getCustomProperty() {
return customProperty;
}
public void setCustomProperty(int customProperty) {
this.customProperty = customProperty;
}
#Override
public Customer getObject() throws Exception {
if (customProperty==1)
return new Customer("1", "One");
return new Customer("999", "Generic");
}
#Override
public Class<?> getObjectType() {
return Customer.class;
}
#Override
public boolean isSingleton() {
return true;
}
}
That is basically it, now based on how you inject in th properties of the factory bean, the actual bean instantiation can be controlled in the getObject method above

How to wire Model View and Controller using Spring

In a swing GUI application, where MVC pattern is applied, how can we use Spring to wire the model view and controller? i.e. what beans (model, view or controller) should be injected using spring and what should be created from the application? I have applied the MVC pattern described here when developing the application. Thanks in advance.
If you have some leeway in the technologies you're using, I'd say you switch to (Griffon)[http://griffon.codehaus.org/]. It uses spring in the background and you also get the power of groovy and Swing UI builders. Best part is, you can still reuse the java code you've written so far. Also, you don't need to worry about DI and stuff. Griffon handles it for you.
On one of my projects, I successfully used Spring Rich Client.
If you are starting from scratch, I suggest that you take a look at it, it worth it. And it also provides some services out of the box (like authentication box and so).
I suggest that you can use "spring mvc".
Jsp(View) controller how to show the data;
Controller controll the return the view required data;
Server controller the system logic;
Model is the database model.
It would come to noone's surprise that I'd recommend you to have a look at Griffon. The MVC pattern is deeply engrained in Griffon's DNA, have a look at this sample app as shown in the Griffon Guide
http://griffon.codehaus.org/guide/0.9.5-rc2/guide/2.%20Getting%20Started.html#2.3%20A%20Groovy%20Console%20Example
Griffon provides basic DI capabilities for each MVC member, you only need to define properties following a naming convention. Services, where you would usually put most of the application's logic, are also automatically injected into controllers, as the guide explains in
http://griffon.codehaus.org/guide/0.9.5-rc2/guide/8.%20Controllers%20and%20Services.html#8.2%20Services
However you can make use of Spring DI too via the Spring plugin
http://artifacts.griffon-framework.org/plugin/spring
Spring beans may be defined using the standard XML approach, annotations or the Groovy Spring DSL.
I defined all the beans in spring and used a factory method to create the views when required. Controller is injected to the view and the model and view are added to the controller via spring.
Following are the code samples from a simple example that I came up with, in order to find a solution: (sorry for the long post!)
the application context file:
<bean id="firstModel" class="com.model.FirstModel"></bean>
<bean id="secondModel" class="com.model.SecondModel"></bean>
<bean id="firstController" class="com.controller.FirstController" />
<bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject">
<ref local="firstController" />
</property>
<property name="targetMethod">
<value>addModel</value>
</property>
<property name="arguments">
<list>
<value>FIRST</value>
<ref local="firstModel" />
</list>
</property>
</bean>
<bean id="secondController" class="com.controller.SecondController" />
<bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject">
<ref local="secondController" />
</property>
<property name="targetMethod">
<value>addModel</value>
</property>
<property name="arguments">
<list>
<value>SECOND</value>
<ref local="secondModel" />
</list>
</property>
</bean>
<bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject">
<ref local="secondController" />
</property>
<property name="targetMethod">
<value>addModel</value>
</property>
<property name="arguments">
<list>
<value>FIRST</value>
<ref local="firstModel" />
</list>
</property>
</bean>
<bean id="firstForm" class="com.view.FirstForm">
<property name="controller">
<ref bean="firstController" />
</property>
</bean>
<bean id="secondForm" class="com.view.SecondForm">
<property name="controller">
<ref bean="secondController" />
</property>
</bean>
following is the abstract controller class:
public class AbstractController implements PropertyChangeListener {
Map<Type, BaseView> registeredViews;
Map<Type, AbstractModel> registeredModels;
public AbstractController() {
registeredViews = new HashMap<Type, BaseView>();
registeredModels = new HashMap<Type, AbstractModel>();
}
public void addModel(Type type, AbstractModel model) {
registeredModels.put(type, model);
model.addPropertyChangeListener(this);
}
public void removeModel(AbstractModel model) {
registeredModels.remove(model);
model.removePropertyChangeListener(this);
}
public void addView(BaseView view, Type type) {
registeredViews.put(type, view);
}
public void removeView(javax.swing.JFrame view) {
registeredViews.remove(view);
}
public void propertyChange(PropertyChangeEvent evt) {
for (BaseView view : registeredViews.values()) {
view.modelPropertyChange(evt);
}
}
protected void setModelProperty(String propertyName, Object newValue) {
for (AbstractModel model : registeredModels.values()) {
Statement statment = new Statement(model, "set" + propertyName, new Object[] { newValue });
try {
statment.execute();
} catch (NoSuchMethodException e) {
continue;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
following is the abstract model class:
public class AbstractModel {
protected PropertyChangeSupport propertyChangeSupport;
public AbstractModel() {
propertyChangeSupport = new PropertyChangeSupport(this);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(listener);
}
protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
}
}
Following is the code sample of the view interface:
public interface BaseView {
void modelPropertyChange(PropertyChangeEvent evt);
public abstract void showForm();
}
following is the code sample of the factory class:
public class FormFactory {
private ApplicationContext context;
private static FormFactory viewFactory;
private FormFactory() {
if (context == null) {
context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
}
}
public static synchronized FormFactory getInstance() {
if (viewFactory == null) {
viewFactory = new FormFactory();
}
return viewFactory;
}
public BaseView createForm(Type type) {
BaseView form = null;
switch (type) {
case FIRST:
form = (BaseView) context.getBean("firstForm");
break;
case SECOND:
form = (BaseView) context.getBean("secondForm");
break;
default:
break;
}
return form;
}
}

Spring 3: disable SpEL evaluation of a bean property value?

We're in the process of updating our apps from Spring 2.5 to 3.0 and we've hit a problem with the new SpEL evaluation of bean properties.
We've been using an in-house templating syntax in one module which unfortunately uses the same "#{xyz}" markup as SpEL. We have a few beans which take string's containing these expressions as properties but spring assumes they are SpEL expressions and throws a SpelEvaluationException when it tries to instantiate the bean.
e.g.
<bean id="templatingEngine" class="com.foo.TemplatingEngine">
<property name="barTemplate" value="user=#{uid}&country=#{cty}"/>
</bean>
Is it possible to disable SpEL evaluation, ideally per-bean, but alternatively for the whole application context?
Alternatively is there a way to escape the values?
Thanks,
Stephen
Completely disable SpEL evaluation by calling the bean factory setBeanExpressionResolver method passing in null. You can define a BeanFactoryPostProcessor to do this.
public class DisableSpel implements BeanFactoryPostProcessor {
public void postProcessBeanFactory(
ConfigurableListableBeanFactory beanFactory)
throws BeansException
{
beanFactory.setBeanExpressionResolver(null);
}
}
Then define this bean in the application context.
<bean class="com.example.spel.DisableSpel"/>
Well what you could do is re-define the expression language delimiters.
I would say the way to do this is through a special bean that implements BeanFactoryPostProcessor (thanks to inspiration by Jim Huang):
public class ExpressionTokensRedefiner implements BeanFactoryPostProcessor{
private BeanExpressionResolver beanExpressionResolver;
public void setBeanExpressionResolver(
final BeanExpressionResolver beanExpressionResolver){
this.beanExpressionResolver = beanExpressionResolver;
}
#Override
public void postProcessBeanFactory(
final ConfigurableListableBeanFactory beanFactory)
throws BeansException{
beanFactory.setBeanExpressionResolver(createResolver());
}
private String expressionPrefix = "${";
private String expressionSuffix = "}";
public void setExpressionPrefix(final String expressionPrefix){
this.expressionPrefix = expressionPrefix;
}
public void setExpressionSuffix(final String expressionSuffix){
this.expressionSuffix = expressionSuffix;
}
private BeanExpressionResolver createResolver(){
if(beanExpressionResolver == null){
final StandardBeanExpressionResolver resolver =
new StandardBeanExpressionResolver();
resolver.setExpressionPrefix(expressionPrefix);
resolver.setExpressionSuffix(expressionSuffix);
return resolver;
} else{
return beanExpressionResolver;
}
}
}
Define it as a bean like this:
<bean class="foo.bar.ExpressionTokensRedefiner">
<property name="expressionPrefix" value="[[" />
<property name="expressionSuffix" value="]]" />
</bean>
or like this:
<!-- this will use the default tokens ${ and } -->
<bean class="foo.bar.ExpressionTokensRedefiner" />
or use a custom resolver:
<bean class="foo.bar.ExpressionTokensRedefiner">
<property name="beanExpressionResolver">
<bean class="foo.bar.CustomExpressionResolver" />
</property>
</bean>
Now you can leave your definitions untouched and if you want to use SpEL, use the new delimiters.
EDIT: now I did test it and it actually works.
<bean class="foo.bar.ExpressionTokensRedefiner">
<property name="expressionPrefix" value="[[" />
<property name="expressionSuffix" value="]]" />
</bean>
<bean class="foo.bar.FooFritz">
<property name="fizz" value="[[ systemProperties['user.home'] ]]"></property>
<property name="fozz" value="[[ systemProperties['java.io.tmpdir'] ]]"></property>
<!-- this is what it would normally choke on -->
<property name="fazz" value="#{ boom() }"></property>
</bean>
Test code:
final ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext("classpath:foo/bar/ctx.xml");
context.refresh();
final FooFritz fooFritz = context.getBean(FooFritz.class);
System.out.println(fooFritz.getFizz());
System.out.println(fooFritz.getFozz());
System.out.println(fooFritz.getFazz());
Output:
/home/seanizer
/tmp
#{ boom() }
I am not a dab, but this mighbe of help.
https://issues.apache.org/jira/browse/CAMEL-2599

Categories