我是thymeleaf的新手。我想在我的spring mvc項目中同時使用jsp和thymeleaf,一切看起來都很完美,但它引發了404異常「請求資源不可用」。請幫忙。如何在同一個彈簧mvc項目中使用jsp和thymeleaf
和我的配置是這樣的:
彈簧的Servlet-config.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.myorg.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
彈簧thymeleaf-配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="cacheable" value="true" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="viewNames" value="thymeleaf/*" />
</bean>
</beans>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>MyThymeLeafProject</display-name>
<context-param>
<param-name>ContectConfigLocation</param-name>
<param-value>tiles3</param-value>
</context-param>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-*-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
控制器
package com.myorg.controller;
import org.apache.catalina.connector.Request;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class MyController {
@RequestMapping(value="/decideView",params="thymebtn",method=RequestMethod.GET)
public String thymview(@RequestParam("name")String name,Model model){
model.addAttribute("name", name);
return "mythymeleaf";
}
@RequestMapping(value="/decideView",params="jspbtn",method=RequestMethod.GET)
public String getView(@RequestParam("name") String name,Model model){
model.addAttribute("name", name);
return "myjspview";
}
}
的index.jsp
<html>
<body>
<h2>Hello World!</h2>
<form action="decideView">
Name:<input type="text" name="name" method="get">
<input type="submit" value="get from Jsp" name="jspbtn">
<input type="submit" value="get from thyme" name="thymebtn">
</form>
</body>
</html>
項目結構:
感謝您的答覆的山姆和marioosh我曾按照你的建議,但它仍然沒有與thymeleaf工作。我同意@sam,它是到達JSP視圖解析器而不是Thymeleaf解析器。當我只有百里酚解決方案時它工作正常,但它不是我想要的。所以我可以做什麼才能使它工作。 –