Basic configuration files looks unintuitive.
If I create simple hello world example, and then rename home.jsp
to home.html
and edit servlet-context.xml
file from
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
to
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".html" />
</beans:bean>
I start to get an error
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/myapp/WEB-INF/views/home.html] in DispatcherServlet with name 'appServlet'
Why? What suffix
property means?
UPDATE
My controller is follows. As you see it does not contain file extension
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
}
Background of the problem
First thing to understand is following: it is NOT spring which renders the jsp files. It is JspServlet (org.apache.jasper.servlet.JspServlet) which does it. This servlet comes with Tomcat (jasper compiler) not with spring. This JspServlet is aware how to compile jsp page and how to return it as html text to the client. The JspServlet in tomcat by default only handles requests matching two patterns: *.jsp and *.jspx.
Now when spring renders the view with InternalResourceView
(or JstlView
), three things really takes place:
"public ModelAndView doSomething() { return new ModelAndView("home") }"
)RequestDispatcher
knows that each *.jsp request should be forwarded to JspServlet (because this is default tomcat's configuration)When you simply change the view name to home.html tomcat will not know how to handle the request. This is because there is no servlet handling *.html requests.
Solution
How to solve this. There are three most obvious solutions:
Initial configuration (only handling jsp)
First let's assume we configure spring without xml files (only basing on @Configuration annotation and spring's WebApplicationInitializer interface).
Basic configuration would be following
public class MyWebApplicationContext extends AnnotationConfigWebApplicationContext {
private static final String CONFIG_FILES_LOCATION = "my.application.root.config";
public MyWebApplicationContext() {
super();
setConfigLocation(CONFIG_FILES_LOCATION);
}
}
public class AppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
WebApplicationContext context = new MyWebApplicationContext();
servletContext.addListener(new ContextLoaderListener(context));
addSpringDispatcherServlet(servletContext, context);
}
private void addSpringDispatcherServlet(ServletContext servletContext, WebApplicationContext context) {
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet",
new DispatcherServlet(context));
dispatcher.setLoadOnStartup(2);
dispatcher.addMapping("/");
dispatcher.setInitParameter("throwExceptionIfNoHandlerFound", "true");
}
}
package my.application.root.config
// (...)
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Autowired
@Qualifier("jstlViewResolver")
private ViewResolver jstlViewResolver;
@Bean
@DependsOn({ "jstlViewResolver" })
public ViewResolver viewResolver() {
return jstlViewResolver;
}
@Bean(name = "jstlViewResolver")
public ViewResolver jstlViewResolver() {
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix("/WEB-INF/internal/");
resolver.setViewClass(JstlView.class);
resolver.setSuffix(".jsp");
return resolver;
}
}
In above example I'm using UrlBasedViewResolver with backing view class JstlView, but you can use InternalResourceViewResolver as in your example it does not matter.
Above example configures application with only one view resolver which handles jsp files ending with .jsp
. NOTE: As stated in the beginning JstlView really uses tomcat's RequestDispatcher to forward the request to JspSevlet to compile the jsp to html.
Implementation on solution 1 - expose the html as a resource file:
We modify the WebConfig class to add new resources matching. Also we need to modify the jstlViewResolver so that it does not take neither prefix nor suffix:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Autowired
@Qualifier("jstlViewResolver")
private ViewResolver jstlViewResolver;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/someurl/resources/**").addResourceLocations("/resources/");
}
@Bean
@DependsOn({ "jstlViewResolver" })
public ViewResolver viewResolver() {
return jstlViewResolver;
}
@Bean(name = "jstlViewResolver")
public ViewResolver jstlViewResolver() {
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix(""); // NOTE: no prefix here
resolver.setViewClass(JstlView.class);
resolver.setSuffix(""); // NOTE: no suffix here
return resolver;
}
// NOTE: you can use InternalResourceViewResolver it does not matter
// @Bean(name = "internalResolver")
// public ViewResolver internalViewResolver() {
// InternalResourceViewResolver resolver = new InternalResourceViewResolver();
// resolver.setPrefix("");
// resolver.setSuffix("");
// return resolver;
// }
}
By adding this we say that every that every request going to http://my.server/someurl/resources/ is mapped to resources directory under your web directory. So if you put your home.html in resources directory and point your browser to http://my.server/someurl/resources/home.html the file will be served. To handle this by your controller you then return the full path to the resource:
@Controller
public class HomeController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView home(Locale locale, Model model) {
// (...)
return new ModelAndView("/someurl/resources/home.html"); // NOTE here there is /someurl/resources
}
}
If you place in the same directory some jsp files (not only *.html files), say home_dynamic.jsp in the same resources directory you can access it similar way, but you need to use the actual path on the server. The path does not start with /someurl/ because this is the mapping only for html resources ending with .html). In this context jsp is dynamic resource which in the end is accessed by JspServlet using actual path on disk. So correct way to access the jsp is:
@Controller
public class HomeController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView home(Locale locale, Model model) {
// (...)
return new ModelAndView("/resources/home_dynamic.jsp"); // NOTE here there is /resources (there is no /someurl/ because "someurl" is only for static resources
}
To achieve this in xml based config you need to use:
<mvc:resources mapping="/someurl/resources/**" location="/resources/" />
and modify your jstl view resolver:
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- Please NOTE that it does not matter if you use InternalResourceViewResolver or UrlBasedViewResolver as in annotations example -->
<beans:property name="prefix" value="" />
<beans:property name="suffix" value="" />
</beans:bean>
Implementation on solution 2:
In this option we use the tomcat's JspServlet to handle also static files. As a consequence you can use jsp tags in your html files:) It's of course your choice if you do it or not. Most probably you want to use plain html so simply do not use jsp tags and the content will be served as if it was static html.
First we delete prefix and suffix for view resolver as in previous example:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Autowired
@Qualifier("jstlViewResolver")
private ViewResolver jstlViewResolver;
@Bean
@DependsOn({ "jstlViewResolver" })
public ViewResolver viewResolver() {
return jstlViewResolver;
}
@Bean(name = "jstlViewResolver")
public ViewResolver jstlViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver(); // NOTE: this time I'm using InternalResourceViewResolver and again it does not matter :)
resolver.setPrefix("");
resolver.setSuffix("");
return resolver;
}
}
Now we add JspServlet for handling also *.html files:
public class AppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
WebApplicationContext context = new MyWebApplicationContext();
servletContext.addListener(new ContextLoaderListener(context));
addStaticHtmlFilesHandlingServlet(servletContext);
addSpringDispatcherServlet(servletContext, context);
}
// (...)
private void addStaticHtmlFilesHandlingServlet(ServletContext servletContext) {
ServletRegistration.Dynamic servlet = servletContext.addServlet("HtmlsServlet", new JspServlet()); // org.apache.jasper.servlet.JspServlet
servlet.setLoadOnStartup(1);
servlet.addMapping("*.html");
}
}
Important is that to make this class available you need to add the jasper.jar from your tomcat's installation just for compilation time. If you have maven app this is realtively easy by using the scope=provided for the jar. The dependency in maven will look like:
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>${tomcat.libs.version}</version>
<scope>provided</scope> <!--- NOTE: scope provided! -->
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>${tomcat.libs.version}</version>
<scope>provided</scope>
</dependency>
If you want to do it in xml way. You would need to register jsp servlet to handle *.html requests, so you need to add following entry to your web.xml
<servlet>
<servlet-name>htmlServlet</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>htmlServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
Now in your controller you can access both html and jsp files just like in previous example. The advantage is that there is no "/someurl/" extra mapping which was needed in Solution 1. Your controller will look like:
@Controller
public class HomeController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView home(Locale locale, Model model) {
// (...)
return new ModelAndView("/resources/home.html");
}
To point to your jsp you are doing exactly the same:
@Controller
public class HomeController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView home(Locale locale, Model model) {
// (...)
return new ModelAndView("/resources/home_dynamic.jsp");
}
Implementation on solution 3:
Third solution is somewhat a combination of solution 1 and solution 2. So in here we want to pass all the requests to *.html to some other servlet. You can write your own or look for some good candidate of already existing servlet.
As above first we clean up the prefix and suffix for the view resolver:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Autowired
@Qualifier("jstlViewResolver")
private ViewResolver jstlViewResolver;
@Bean
@DependsOn({ "jstlViewResolver" })
public ViewResolver viewResolver() {
return jstlViewResolver;
}
@Bean(name = "jstlViewResolver")
public ViewResolver jstlViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver(); // NOTE: this time I'm using InternalResourceViewResolver and again it does not matter :)
resolver.setPrefix("");
resolver.setSuffix("");
return resolver;
}
}
Now instead of using the tomcat's JspServlet we write our own servlet (or reuse some existing):
public class StaticFilesServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
String resourcePath = request.getRequestURI();
if (resourcePath != null) {
FileReader reader = null;
try {
URL fileResourceUrl = request.getServletContext().getResource(resourcePath);
String filePath = fileResourceUrl.getPath();
if (!new File(filePath).exists()) {
throw new IllegalArgumentException("Resource can not be found: " + filePath);
}
reader = new FileReader(filePath);
int c = 0;
while (c != -1) {
c = reader.read();
if (c != -1) {
response.getWriter().write(c);
}
}
} finally {
if (reader != null) {
reader.close();
}
}
}
}
}
We now instruct the spring to pass all requests to *.html to our servlet
public class AppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
WebApplicationContext context = new MyWebApplicationContext();
servletContext.addListener(new ContextLoaderListener(context));
addStaticHtmlFilesHandlingServlet(servletContext);
addSpringDispatcherServlet(servletContext, context);
}
// (...)
private void addStaticHtmlFilesHandlingServlet(ServletContext servletContext) {
ServletRegistration.Dynamic servlet = servletContext.addServlet("HtmlsServlet", new StaticFilesServlet());
servlet.setLoadOnStartup(1);
servlet.addMapping("*.html");
}
}
The advantage (or disadvantage, depends on what you want) is that jsp tags will obviously not be processed. You controller looks as usual:
@Controller
public class HomeController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView home(Locale locale, Model model) {
// (...)
return new ModelAndView("/resources/home.html");
}
And for jsp:
@Controller
public class HomeController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView home(Locale locale, Model model) {
// (...)
return new ModelAndView("/resources/home_dynamic.jsp");
}
Resolver class is used to resolve resources of a view class, view class in turn, generates the views from resources. For example, with a typical InternalResourceViewResolver as below:
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
A view name "home" will be mapped as "/WEB-INT/views/home.jsp" and then translated into a JSP view using view class InternalResourceView (which is for JSP). If you replace the suffix value with ".html", Spring can get the specific resource "/WEB-INT/views/home.html" but doesn't know how to generate it.
Plain .html files are static and do not need a special ViewResolver. You should set up a static folder for your html pages as shown here.
For example:
<mvc:resources mapping="/static/**" location="/static/" />
well, it seems you didn' t set the view' s order.
for example, if your project has view like jsp, json, velocity, freemarker, etc. you can use all of them(maybe you need new version of spring, 3.1+), but only one view will be select to render to client, that depends on your view' s order, the lower the order, the prefer the view.
for example, you set jsp view' s order is 1, and freemarker view' s order is 2, both of their view name is "home", the spring will choose view.jsp(if you set suffix to .jsp). Well, if your view name is "index", no index.jsp but index.ftl(suppose you set freemarker' s view to .ftl), spring will choose the later.
the sample code using spring' s java config, you can easily convert to xml-style.
@Bean
public InternalResourceViewResolver jspViewResolver() {
InternalResourceViewResolver jsp = new InternalResourceViewResolver();
jsp.setOrder(4);
jsp.setCache(true);
jsp.setViewClass(org.springframework.web.servlet.view.JstlView.class);
jsp.setPrefix("/WEB-INF/jsp/");
jsp.setSuffix(".jsp");
return jsp;
}
@Bean
public FreeMarkerViewResolver freeMarkerViewResolver() {
FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver();
viewResolver.setCache(true);
viewResolver.setPrefix("");
viewResolver.setSuffix(".ftl");
viewResolver.setContentType(ViewConstants.MEDIA_TYPE_HTML);
viewResolver.setRequestContextAttribute("request");
viewResolver.setExposeSpringMacroHelpers(true);
viewResolver.setExposeRequestAttributes(true);
viewResolver.setExposeSessionAttributes(true);
viewResolver.setOrder(2);
return viewResolver;
}
please see the setOrder() method!
the json, jsonp and other type of views may use ontentNegotiation, and you can find it on spring' s docs.
finally, the html view, I mean, totally static files, which is not support by spring default. I suppose the static file desn' t need render by java. you can use the static mapping using the code below:
<mvc:resources mapping="/static/**" location="/static/" />
or use java config:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
int cachePeriod = 3600 * 24 * 15;
registry.addResourceHandler("/static/**").addResourceLocations("/static/").setCachePeriod(cachePeriod);
registry.addResourceHandler("/favicon.ico").addResourceLocations("/").setCachePeriod(cachePeriod);
registry.addResourceHandler("/robots.txt").addResourceLocations("/").setCachePeriod(cachePeriod);
}
and in your @RequestMapping method, you should redirect it!
well, if you don' t want redirection, just set the html view to an dynamic view (freemark, velecity, etc), which will be ok!
hope it useful!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With