Spring MVC - viewResolver - spring

Here is my situation, I am tasked to enhance an existing app which is written in DOJO/Spring MVC.
Looking at existing code, app-servlet.xml file uses
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>.
There are no other view mappings (existing app very simple navigation, mostly index.jsp per component etc).
The new functionality I need add required navigating between multiple pages. My question is,
how can I add new view resolver/mappings without impacting existing application.
Any help is greatly appreciated!
Thanks

You can add another implementation of ViewResolver to your context. Have it implement org.springframework.core.Ordered if needed and set the order to Ordered.HIGHEST_PRECEDENCE. This view resolver could handle views specific to your case. If other viewResolver should resolve the view name have resolveViewName() return null so it will use the next ViewResolver in the chain.

Related

how to implement spring support for custom template engine?

I've decided to use custom template engine with Spring MVC framework.
my templates implemented in java and have method for rendering into String:
public String render(Map context);
how to configure spring to make them available in Controller beans as views, for example like:
ModelAndView modelAndView = new ModelAndView("activationPage"); // - view name which will actually be java class name reference.
modelAndView.addObject("validationResult", validationResult);
return modelAndView;
Model will be passed as context in code connecting spring and my template engine.
You need to implement org.springframework.web.servlet.View (which should be easy, you already have something very similar to the render method it needs), as well as org.springframework.web.servlet.ViewResolver, which maps the view names (e.g. "activationPage") on your custom views.
Once you have that, drop a bean of your ViewResolver class into the context, and (unless you've done something else that gets in the way) it should automatically be picked up by Spring and should just work. if you have other ViewResolvers already in there, they may get into a fight over who gets to resolve the view, in which case ask a new question.
Hi I am the author of Rythm template engine, about half year ago I am having the same requirement like you. What I did is to read the source code of Velocity and Freemarker view of SpringFramework. And then create the Rythm view for spring following their approach.
It's easy to follow something that is already there, and it makes your implementation in good quality to follow the official module. Good luck on you :-)

Spring overwriting controller

I provide a highly customisable application to my clients which is working totally by itself. But If one my client wants to overwrite any Controller, I want to replace my implementation by theirs. However just overwriting the controller causes an ambiguous definition of mappings.
I have been using Component Scanning to load beans.
The potential solutions came to my mind are:
Using component scanner with excluding by a custom filter? (This seems not so easy)
Using a xxxxPostProcessor to remove some beans? (How?)
Any help?
If I got your Question properly,
You can differ implementation by changing URL to particular Implementation name
Say Telecom is interface and AirtelImpl and RelianceImpl are Controllers then
Your request mapping
#RequestMapping(value= "/airtel/doBilling")
#RequestMapping(value= "/reliance/doBilling")
In this way, Implementation flow will differ.
I have followed these steps:
Created a custom annotation: #Devoted
Created a custom ImportBeanDefinitionRegistrar. Iterated already registered bean definitions to find out `#Devoted #Controller's and removed them.
Based on a request I will provide implementation details.

Spring - Adding element(checkbox) to Spring login page (with Spring-security)

In my web application I am using Spring login form (with Spring-security). By default the login form has the fields j_username and j_password. I need to add one more element(checkbox for Terms&Conditions). The current code doesn't have LoginForm as well as LoginController since Spring is internally handling it.
Can anyone please tell how to handle/override this?
I have seen this link Spring security custom login page
But I need to add the new element in LoginForm (which is not existing currently) - where I need to add this new element(in Form - .java file)
Also should I write a new controller (LoginController) or can I use any existing filter as given here? http://static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#filter-stack
Does the user just have to check the box in order to procede, or does it bind to a backing model object.
If it's the former, I'd just handle it through javascript. If the latter, the easiest way would probably be implementing an Authentication Filter, this area of the documentation might help:
http://static.springsource.org/spring-security/site/docs/3.0.x/reference/core-web-filters.html#form-login-filter

Spring and Struts 2 integration

This is a design level question and i need opinion on what would be a better approach.
The application i am working on uses Struts 2 and Spring (for dependency injection).
Each Action class make a call to Service Layer to perform business functions. All data is saved in Model classes. Every action uses modals to save/edit data. These Modal classes are defined as private members of the class with getter/setters.
Question 1) Should we define the Modal classes as Beans in application context? Currently i have not. On form submit the Struts itself creates the instance. On edit ( when i want to show data on screen ) I have to explicitly create the modal ( using new ). What would be the better approach.
Question 2) Should the beans for Action Classes in aplCntx be defined as scope="prototype" ? Does Struts its self not take care to create new instance of action classes?
Question 1:
As Jigar said you don't need to define your action fields as beans in your application context, because most of the time they just carry data between the page and your application, so most probably you either has created them from you service layers and just want to pass them to page for presentation or their data is submitted from the page in which case struts2 takes care of the instantiation.
Question 2:
There's a object factory in struts2 which by default takes care of action creation. You can change this and specify spring to take care of the action creation. First you have to add spring plugin for struts2 to your classpath then add this line to you struts2 config file:
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory"/>
Then you create beans for your actions like:
<bean id="myActionBean" class="com.my.myAction" scope="prototype">
... required properties ...
</bean>
Remember you have to set the scope to prototype that's how it works for struts2. Then in your struts config file:
<action name="myaAction" class="myActionBean">
... required result mapping ...
</action>
1) Should we define the Modal classes as Beans in application context? Currently i have not. On form submit the Struts itself creates the instance. On edit ( when i want to show data on screen ) I have to explicitly create the modal ( using new ). What would be the better approach.
No. You shouldn't define modal in spring-context , they should be just simple POJOs

Multiple View resolvers in spring mvc

I want to use multiple view resolvers in my web app based on spring mvc
Can anyone tell me how do I achieve that.
I want to use both JSP and freemarker in my app.
Please suggest some approaches or links or examples..
All help is appreciated.
Adhir
You can add as many view resolvers as you want. You can specify the order in which the view resolvers need to be checked. Spring will take the first view resolver which can successfully resolve the view.
ex:
Since you have JSP and freemarker add the view resolvers for both and give the order property 1 for JSP and 2 for freemarker.
If your view is /freemarker/hello.ftl then the JSP resoplver will fails since it will not be able to find th file /freemarker/hello.ftl, then the freemarker resolver will handle this view. But if the JSP resolver is able to find the file and resolve it then freemaker resolver will not be used to resolve that view
Refer: Chaining ViewResolvers

Resources