default view resolution in spring - spring

I am a spring newbie and have a question about view resolution. I am changing a webapp that I downloaded online and it uses the simple view resolver strategy:
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
and I keep getting 404 errors for view resolution and I am suspecting that it uses some sort of rewriting / filtering mechanism. Is there a log that I can view in tomcat / Spring class that I can override in order to understand what file is Spring trying to look for when resolving an incoming request?
I understand the operation of InternalResourceViewResolver which strips file name extensions. But what if the request does not have an extension? For instance:
#RequestMapping("/foo")
protected ModelMap render() { return new ModelMap(); }
Then what is the view name that will be resolved to in this case?

See this link for log4j integration
Spring-mvc doesn't use any file for request handling, it uses controllers and requestmapping to map the request to a controller and respective method if any.
The InternalResourceViewResolver that you have written Resolves “view name” that is returned from the controller class to a JSP page residing in /WEB-INF/view/ directory.
an example
#Controller
public class SimpleController{
#RequestMapping("/home")
public String homeMapper(Model model) {
return "home";
}
}
here homeMapper method will be called if you try to access "home" and as it returns home the relative jsp to be rendered is "home.jsp" should be present in /WEB-INF/view/
For more information see spring mvc reference or any tutorial.

Related

View not getting resolved in Spring mvc gradle application

web application is made with gradle sts project
i've added view Resolver like this
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".html" />
</bean>
it is hitting the url but wont return any view
#Controller
public class HomeController {
#RequestMapping(value = DatahubClientConstant.CLIENT_URL , method = RequestMethod.GET )
public ModelAndView getTestPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
//System.out.println("Hello World");
return new ModelAndView("home");
}
}
Tried to sysout it works
It doesnt return any view?
After Some Research i have found out that InternalViewResolver does not resolve html pages directly that's why i was not able to get the view.
Spring MVC ViewResolver not mapping to HTML files
This was a helpful question in resolving the issue. All it is doing is loading the html as static content.

Spring not redirecting to html page

Here is my view resolver:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".html"/>
</bean>
I also have a controller that should return hello.html:
#Controller
#RequestMapping("/")
public class IndexController {
#RequestMapping(method = RequestMethod.GET)
public String getHtmlPage() {
return "hello";
}
}
When I access localhost:8080 I get an error message:
WARNING: No mapping found for HTTP request with URI [/WEB-INF/pages/hello.html] in DispatcherServlet with name 'mvc-dispatcher'
Now when I change my suffix value to .jsp, then hello.jsp is returned correctly.
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
hello.jsp and hello.html are in the same folder. Any ideas how to fix this?
EDIT:
For html you don't need view resolvers. Instead just create a folder in your webapp folder.
Call it static for example , then add <mvc:resources mapping="/static/**" location="/static/" /> to your xml.
In controller instead of return "hello"; put return "static/hello.html";
As a complement to answers of How to map requests to HTML file in Spring MVC?, I would say that the InternalResourceViewResolver knows how to render JSP or JSP+JSTL views. You could of course use HTML views - even if it makes little sense(*) - by writing a custom ViewResolver that would be able to render plain HTML pages.
(*) Normally, the controller prepares a model that will be used by the view. In a plain HTML view, you cannot use the model. Normally when you try to do that, you actually needs to redirect to a plain HTML page, not use it like a view.
To redirect to the HTML page, you put it out of the WEB-INF folder, in a place accessible as a resource.
But in your particular need, you are trying to display hello.html for the root URL. The best way to do it is to put it directly at the root of the web application folder atn declare it in the welcome-file-list of the web.xml :
<welcome-file-list>
<welcome-file>hello.html</welcome-file>
</welcome-file-list>

Can I combine #controller and XML bean mapping in spring?

I currently have a #Controller declared in spring and have a bunch of mappings done like so:
#RequestMapping(value = "foo", method = RequestMethod.GET)
public ModelAndView foo() {
ModelAndView mav = new ModelAndView(
"myjsp");
return mav;
}
However every time I want to add a simple JSP mapping I need to recompile and build a new war and deploy.
This isnt so bad except sometimes other members of the team have requests and it would be easier if they can just go into the test env and create the mapping themselves without having to recompile.
I know that you can do similar mapping using xml but can I do this at the same time that I have the #Controller defined?
Like in the example above how could I define that mapping in XML rather than in java?
or say I needed foo2 to map to myjsp2.jsp
I am using spring MVC 3.2
Look into BeanNameUrlHandlerMapping which allows you specify url patterns for controllers in your configuration. Documentation
Example
<beans>
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean name="/editaccount.form" class="org.springframework.web.servlet.mvc.SimpleFormController">
<property name="formView" value="account"/>
<property name="successView" value="account-created"/>
<property name="commandName" value="account"/>
<property name="commandClass" value="samples.Account"/>
</bean>
<beans>

Mapping jsp for a url in Spring 3 without using controller

How to map a jsp for a url in spring 3 without requestmapping to any controller.
eg. /login to login.jsp without having any userdefined controller in between
Like URLFILENAMECONTROLLER in spring2.5, similarly in spring 3
You can use this paragraph from Spring docs for reference. In short, you can do in several ways one of them with view-controller annotation. The other way when using Java Config:
#EnableWebMvc
#Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
}
}
Where the code maps request for /login to /WEB-INF/views/login.jsp view if the view resolver is defined as in the previous answer.
You can do this:
<mvc:view-controller path="/login" view-name="login"/>
Assuming that you have defined a ViewResolver, something like this:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
This will resolve a request to /login to a /WEB-INF/views/login.jsp page

Spring 3 #Controller is not being invoked for Get request

I am attempting to use Spring 3' MVC support for annotated controllers in my web application.
In my application-context.xml, I've added the following:
<mvc:annotation-driven />
<context:component-scan base-package="com.abc.def.etc"/>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
<property name="order" value="1" />
</bean>
My Controller is annotated as follows:
#Controller
#RequestMapping("/optimizerRules")
public class OptimizerRulesController {
private OptimizerRulesService optimizerRulesService;
private static final Log LOG = LogFactory.getLog(OptimizerRulesController.class);
public OptimizerRulesController()
{
LOG.info("Initializing OptimizerRulesController");
}
#RequestMapping(method = RequestMethod.GET)
public ModelAndView getRuleAttributesAndRules(ModelMap model)
{
LOG.info("Entering getRuleAttributesAndRules method");
}
When I start up my application I can see in my logs that the OptimizerRulesController has been initialized. I can also see the following:
Creating instance of bean 'optimizerRulesController'
Initializing OptimizerRulesController
Mapped URL path [/optimizerRules] onto handler 'optimizerRulesController'
However, when I invoke my application using http://localhost:8080/appName/optimizerRules I get a 404 error!
What configuration am I missing here?
Thanks
Spring MVC would normally log that no mapping is found for a particular request at WARN level. Assuming you're not seeing that in your logs and assuming that WARN is enabled, and since you're not seeing your own log statement, it sounds like your request isn't even hitting the Spring MVC DispatcherServlet, which probably means the URL is wrong.
The URL should be http://server:port/war/dispatcherServletMapping/optimizerRules, so your web.xml should tell you the missing path component, if my assumptions are valid.

Resources