Is it possible to make auto url mapping in Spring MVC? - java

Is it possible to make auto url mapping in Spring MVC? For example, url http://localhost/eshop/products invokes ProductsController in eshop module without explicit #RequestMapping. System should know that the first param is a module and the second param is a controller. I don't want to do any changes in web.xml or in #RequestMapping each time I create a new controller.

Please check this link
http://static.springsource.org/spring/docs/3.0.0.M3/reference/html/ch16s10.html
Convention over configuration with Spring MVC

Related

Mapping URL parameters with hyphens to object in Spring Web MVC without extending ServletModelAttributeMethodProcessor

Ideally request parameters should be case insensitive.
How can request parameters with hyphens be mapped to a java POJO without extending ServletModelAttributeMethodProcessor in Spring Web MVC?
Tried some common solution:
Mapping URL parameters with dashes to object in Spring Web MVC
This works fine.
Trying to find some other workaround.....

Spring boot showing status instead of index

So I updated to a newer version of Spring (now using 1.5.8 from 1.3.0.M2).
Now when I go to the / mapping it shows the Spring status page:
All other mappings work fine, it seems the root mapping doesn't work well, as if it's being overriden somewhere.
#RequestMapping(value="/")
public ModelAndView index() {
return new ModelAndView("index");
}
This was actually returning "index.jsp" before but now is showing me the above page.
Any guidance on what to do to get my main / mapping back to index.jsp?
Spring MVC open index.jsp on "/". I guess this has been answered in the above link where it says we need to define the method signature in the #RequestMapping something like this #RequestMapping(value="/", method = Requestmethod."GET")
have you added this two lines to your property file
# view resolver--------------------------------------
spring.mvc.view.prefix= /WEB-INF/jsp/
spring.mvc.view.suffix= .jsp
Those are spring boot endpoints. check here for more
By default, all endpoints except for shutdown are enabled. If you
prefer to specifically “opt-in” endpoint enablement you can use the
endpoints.enabled property. For example, the following will disable
all endpoints except for info:
endpoints.enabled=false
endpoints.info.enabled=true

How to get spring post param in a form with any name

I want to get parameter from POST method in Spring with name that starts with "value". And it can be like "value_345465". Any ideas?
There is nothing Spring MVC specific. You can use ServletRequest.getParameterNames(), ServletRequest.getParameterMap(), and ServletRequest.getParameter() methods to accomplish this task.

Should I be using #Controller classes when doing Spring web flow

It seems like I can do everything that the Controller class do inside Spring Web-Flow, for example decision making and switching from page to page. To my understanding, its the C inside the MVC model. Am I correct about this?
So my question is, is there any advantage to still include a Controller class when using Spring Web-Flow?
If you need access to the request and response, an appropriate design might still include a controller while also having a flow.xml. For example, if you had this code:
HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getNativeRequest();
HttpServletResponse response = (HttpServletResponse)context.getExternalContext().getNativeResponse();
It's more intelligible to put that in a controller rather than a service.
Also, if you want to register a custom editor, it might make sense to have the controller have this logic in the initBinder() method.
Spring Web Flow uses the Spring MVC framework. The DispatcherServlet handles the request. A FlowHandlerMapping is used to map the request to a particular Web Flow.
Web Flow is to solve the problem involved with controller logic that spans multiple-page navigation (a pageflow, or wizard).
Web Flow can eliminate the need for specialized controller classes to accomplish following a path of page transitions/form updates along a predefined workflow. If you don't need to do this, you can save yourself a lot of configuration/complexity just by using MVC.

Spring MVC: Log Controller paths without params

I am trying to add some metric gathering to a Spring MVC app. Lets say I have a controller whose mapping is:
/User/{username}/Foobar
I want to gather metrics on all controller mapping invocations with the path. Right now I can create a handler/interceptor and look at the requests but that will give me:
/User/Charlie/Foobar
Which is not what I want. I want the controller mapping itself to log. and I don't want to have to add something to every controller. I'd also rather not use AOP if I can help it.
It turns out that Spring hangs the best matching controller pattern on the request itself. You can get this from within a handlerinterceptor like this:
(String)request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE)
I can think of two choices:
It seems to me the results of the matching are obtained in the class org.springframework.web.servlet.handler.AbstractUrlHandlerMapping, which logs the patterns obtained (see line 266). I'd try enabling logging for that class and see if the output is helpful for your purposes.
(Complicated)
Extending org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping to override the lookupHandler method inherited from AbstractUrlHandlerMapping and logging/registering what you need. Accoding to this class documentation, you can register a different one so that the DispatcherServlet uses your version.
In Spring 3.2.x DefaultAnnotationHandlerMapping is deprecated so, a different class would have to be used.

Categories