Spring MVC Portlet DefaultAnnotationHandlerMapping - Choosing Controller Class - spring-mvc

I am trying to use DefaultAnnotationHandlerMapping for annotated Controllers in Spring Portlet MVC.
My portlet works with one controller. But I am not able to understand as to how DefaultAnnotationHandlerMapping is able to determine which Controller to choose among many controllers in the same package.
This link says that it searches for best matching Controller. But I could not understand the rules to determine the best matching controller.
Any clues?

Beware of the bug in Spring 3.1.2. I explained some portion of what you asked in Is this a Bug in Spring 3.1.2 ( specifically Spring Portlet MVC's )?
I also logged at bug https://jira.springsource.org/browse/SPR-9697 .
Hope you find it useful.

Related

What specific use cases would call for a servlet filter outside of Struts2 but within the same web application?

This question was asked in an answer Difference between Interceptors and Filters - Is this right?, but it remained unanswered.
Can anyone please provide some real time examples on this?
As we can use both Filter and Interceptor for Pre-processing and Post-processing
of a request.
What would be the use cases(scenarios) where servlet filter is used outside of Struts2 but within the same web application?
I had some issues combining Apache Shiro (a login framework) with a Struts2 webapp. Because Struts2 was handling the actions of the login (ie using s: tags), a lot of default stuff on Shiro broke such as built-in email validations, remember me and so on.
This also caused Struts2 validation to stop working on Shiro pages (login and createuser) along with JDBC connection problems as I had Struts2 performing actions on sessionfactories built from hibernate.cfg.xml ORM configs, and non ORM connections in Shiro.ini.
In combination with other frameworks, sometimes you don't want Struts2 to handle the actions, as you will have certain issues like I described above. I'm sure there are other examples but this was something I experienced. I lost a lot of built in features provided by Apache Shiro because I had struts handling the actions.

Is there a way to use AssertJ assertions with Spring MVC Test?

I have been using AssertJ for some time in my projects. Recently I started using Spring MVC Test for testing Spring MVC controllers.
But I am not getting how to use AssertJ with it. All examples I see online all use Hamcrest with Spring MVC Test.
Below is an example using the Hamcrest API.
mockMvc
.perform(get("/user?operation=userList"))
.andExpect(status().isOk())
.andExpect(model().attribute(UserController.MODEL_ATTRIBUTE_USER_LIST, userList))
.andExpect(view().name(UserController.VIEW_USER_LIST))
.andExpect(model().attribute(UserController.MODEL_ATTRIBUTE_USER_LIST, hasSize(2)))
.andExpect(model().attribute(UserController.MODEL_ATTRIBUTE_USER_LIST, hasItem(
allOf(
hasProperty("id", is(1L)),
hasProperty("description", is("Lorem ipsum")),
hasProperty("title", is("Foo"))
)
)))
.andExpect(model().attribute(UserController.MODEL_ATTRIBUTE_USER_LIST, hasItem(
allOf(
hasProperty("id", is(2L)),
hasProperty("description", is("Lorem ipsum")),
hasProperty("title", is("Bar"))
)
)));
Update
If you would like to vote for inclusion of support for AssertJ assertions with MockMvc, please see the related Spring JIRA issue: SPR-16637.
Generally speaking, you may choose whatever assertion framework you like when testing with Spring.
However, the particular scenario you are describing involves the API of the Spring MVC Test framework. The methods in question are designed to be used with the Hamcrest Matcher API. It is therefore not possible to use AssertJ within those method calls.
Regards,
Sam (author of the Spring TestContext Framework)
I've put together a library that offers AssertJ assertions for MockMvc but also for ResponseEntity (returned by TestRestTemplate): https://github.com/ngeor/yak4j-spring-test-utils
There has recently been an issue raised on the Spring Boot project to discuss adding support for AssertJ assertions with MockMvc, it might be worth keeping an eye on it. You can view the issue here: https://github.com/spring-projects/spring-boot/issues/5729
It looks like the initial concept created by Phil Webb involves wrapping the MockMvc to provide support for AssertJ assertions.

Spring MVC: in which situation would be useful to have several DispatcherServlet - Contexts?

Spring MVC documentation says that it is possible to have several DispatcherServlets in a Web application, each one would have a separate context. I wonder in which situation this would be useful? Also, why separated contexts? Could someone give some insight about this?
Thx in advance.
Please follow this question.
Can I use Spring MVC and Spring WS in one single application?
This should answer your question.
Please follow another question
Spring - When should I consider loading another context in same JVM?
This should answer your second question.
Yea, Its possible to have several Dispatcher Servlet, Say for example in one of my project I m using Json, Ibatis and normal spring configuration. For each one I m using each dispatcher servlet. But the main dispatcher servlet would be one in which other two would be included.
Its just for convinient.

Ant and XML-based Spring security integration (without using annotations)

I am newbie to Spring .I built a spring MVC application which is XML based and I used JAR files using Spring Framework MVC application step-by-step.
Now I have to integrate spring security in it. I found many tutorials for spring security, but all are made using annotations. And in the MVC tutorial it uses Ant and XML. I am just lost in this part. Should I have to convert my whole application using annotations or what to do?
Is there any way using spring security without annotations? May be this question is not logical. But guide me and help me solve my confusion.
Or is there any tutorial help me in this regard, please let me know. Thank you
I'm not sure what you mean with "change my application to annotation base", but you will have to add some anotations above your methods like this:
#Secured("isAuthenticated() and hasRole('PERMISSION_BUY_ITEMS')")
public void buyItem(int itemId, int userId) {
// you buy stuff login
}
This way in order to access your method the user should be logged in (authenticated) and have the permission BUY_ITEMS.
You don't have to do this for all your methods.
If you do not need permission/role based authorization you can just use authentication based expressions like isAuthenticated(), isAnonymous() and permitAll out of the box without any custom implementation.

compare spring mvc 3 and struts 2

Just about to start a java EE project.(Biz requirement is under change still.)
For the web tier, we looked at various java web frameworks and eliminated component-based ones such as JSF, Wicket. Now it comes to spring mvc 3 or struts 2.
Googled it and found little useful info. Can anyone talk about their pros and cons? Thanks.
I took over a Struts 2 + Guice web app that used the REST plugin to do convention over configuration. It was very easy to work on at first but I ran into a couple of hurdles that were either difficult or impossible to overcome.
One of these was that I needed to have internal dot/period characters in the path of the URI and Struts 2 + REST did not allow this, as it would interpret the dot to indicate a file extension and try to marshall to the appropriate view (e.g. like catching .xml and .json).
So I ended up rewriting the webapp in Spring 3 and was able to fix all those issues I couldn't handle in Struts 2. I've been much happier with Spring 3 and found it just as fast to code in as Struts 2. I've stuck with annotation based configuration as much as possible and tried to use the JSR versions where ever possible (330 #Inject and 303 #Valid, etc), so that if I decide to get rid of Spring I am not stuck with custom Spring annotations.
My vote is Spring 3.
In this other stackoverflow question you have a lot of answers comparing struts and Spring. Though many of them don't mention explicitly the version 3 of Spring the comparison would be similar to the version 2.5.
As many of them say, I'd prefer Spring. It makes things easier when you use annotations. One fact I don't like in Struts 2 in comparison with Spring-mvc is that you have to add getters and setters for every property you want to get in the actions. I think Spring is cleaner in this way.
#Javi
You have various options in Struts2 to avoid these getter and setter one of them id ModelDriven interceptor
Well my vote is for Struts2 since i am working on it from so long but this does not mean Spring MVC is bad i have worked on it also and its equally good..
Choice is all yours and it depends what word you like most Spring or Struts

Resources