I'm a beginner with Spring and I'm trying to understand how a controller works.
I have several methods annotated with #RequestMapping and everything works fine, but I don't know how to handle simple page requests.
Suppose I have to redirect from one page to another, without making server logic. If I create the url redirecting to PageA.jsp it seems that I always need to define the method catching the request in the Controller, declaring a method that is pratically empty, like this:
#RequestMapping(value="/PageA.jsp")
public String redirectToPageA(){
return "PageA";
}
If I don't follow this approach I get 404 error as the controller can't find the mapping. I don't like this approach very much as it fills my controller with useless empty methods.
Which is the suggested approach?
I suggest using a view resolver.
Read more about it here - 17.5.1 Resolving views with the ViewResolver interface from 17. Web MVC framework.
In that section you have
As an example with JSP as a view technology, you can use the
UrlBasedViewResolver. This view resolver translates a view name to a
URL and hands the request over to the RequestDispatcher to render the
view
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
When returning test as a logical view name, this view resolver
forwards the request to the RequestDispatcher that will send the
request to /WEB-INF/jsp/test.jsp. including the code.
And this answer has the annotation based configuration of a view resolver (except you could use the UrlBasedViewResolver).
Related
In Spring MVC, when request commes, DispatcherServlet specifies HandlerMapping that indicates which controller should process request. Controller processes request and returns model with specified View.
And I dont get what happens after that, when ViewResolver starts his work. If View is already specified by Controller, why there is a ViewResolver layer? What does it actually do? Shouldn't View been specified before or paraller with Controller? Or maybe Controller should trigger ViewResolver layer?
The ViewResolver maps view names to actual views.when you have for example:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
you tell spring in controller method when you return a string, add prefix and suffix to the return value and the load that view, lets say you have
#RequestMapping("/")
public String home() {
return "home";
}
ViewResolver will take home and change it to home.jsp and will look at /WEB-INF/views/ to load /WEB-INF/views/home.jsp
I have a Spring project. When I call the URL it is calling the controller and printing the result in console. But the requestmapping method prints the output but not displaying the view / jsp page.
Please find the below link for better understanding.
My Spring Project Structure:
the controller is called and printing 'IN' as output:
The controller i been using.
#Controller
public class BaseController {
#RequestMapping(value="/")
public String getRespnse(Model m) {
System.out.println("IN");
return "home";
}
}
This the view resolver I'm using.
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
I cannot able view the jsp output in browser.Please suggest a way to handle.
If you map your DispatcherServlet to url pattern /* in your web.xml it overrides all other url mappings specified. To handle JSPs web containers includes a mapping *.jsp to handle JSPs.
So you mapping overrides the container`s *.jsp servlet and requests for servlets are now going through your DispatcherServlet which doesnt know how to handle JSPs.
Change your url-pattern from /* to /
Im newbie to Spring framework. I started learning few basic things from this link. I was stuck with this topic. What is the difference b/w View and Index. Its not much clear in tat doc
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
You see that indexController extends PrameterizedViewController which is a basic controller that always returns a named view. The advantage of this controller is that the client is not exposed to the concrete view technology but only to the controller url.
ViewResolver is of type InternalViewResolver, this means it will resolve views such as jsp, servlet etc .
So you would use viewResolver to link to another jsp or servlet and the indexController would be used in order for your request to be processed by a #Controller class.
the title of this post might be confusing.I'll try be best to clarify it.
I Started a project using Spring MVC it works fine no problem.After that i've realized
that i was a bit overkill and found that i needed a front controller dispatcher because
all a wanted was nice urls without extensions.
So instead of implementing a whole new font controller i will like to take advantage of the existing Spring MVC setup. here is an example of a controller
#RequestMapping("/accounts")
public String home() {
return "accounts";
}
#RequestMapping(value="/")
public String Home(){
return "home";
}
as you can see, the return string is the one indicating the view based on the resourceviewresolver
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/" />
<property name="suffix" value=".jsp" />
</bean>
now i don't want to use the controller, but need a mechanism to map the request to a physical page.i'm wondering if that could be possible.
How can i do that? i should simply create another dispatcher?
thanks for reading this and helping out.
Spring 3 supports <mvc:view-controller> element for such cases:
<mvc:view-controller path="/" view-name="home" />
<mvc:view-controller path="/accounts" view-name="accounts" />
See also:
MVC Simplifications in Spring 3.0
I want to define a set of error messages so that when validation errors generate codes, those codes pick up the corresponding error message and print them.
For the sake of learning, and to develop an extendable web app, I'd like to follow the proper i18n path, though I only need to define one (english) set of messages now.
So the locales should all default to english when they don't find their own resources (which I'm yet to define).
I've never used any of the i18n functionality of Java. And the spring docs assume that I have this knowledge.
Could someone just give me a gental nudge in the right direction?
I've defined a messageSource in my dispatcher-servlet.xml webapp context. I have a validator which produces a BindingResult object with a rejected field "username", with code "username.taken". I can generate the default message.
Now I just need to get the error message from the errormessages.properties file in my view.
How do I resolve an error code to a message?
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>errormessages</value>
</list>
</property>
</bean>
It depends on what you want to do with this text. The first possibility is to get the message programmatically:
#Autowired
private MessageSource messageSource;
#RenderMapping(params = "render=details")
public String showDetails (Model model, Locale locale) {
messageSource.getMessage(<your key goes here>, null, locale);
}
This way is very uncommon, cause you have to get the message keys form the Errors object by yourself.
Another more common way is to use the build in view extensions shipped with spring mvc. You didn't wrote it but I guess your using JSPs. In that case you can simply write something like this in your JSP:
<!-- showing errors -->
<div>
<form:errors path="*" />
</div>
<!-- showing arbitrary messages -->
<div>
<spring:message code="${success.messageKey}"/>
</div>
For further reading I suggest you http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/view.html