2016-10-06 50 views
-1

我是thymeleaf的新手。我想在我的spring mvc項目中同時使用jsp和thymeleaf,一切看起來都很完美,但它引發了404異常「請求資源不可用」。請幫忙。如何在同一個彈簧mvc項目中使用jsp和thymeleaf

404 exception!

和我的配置是這樣的:

彈簧的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> 

項目結構:

Project Structure

+0

感謝您的答覆的山姆和marioosh我曾按照你的建議,但它仍然沒有與thymeleaf工作。我同意@sam,它是到達JSP視圖解析器而不是Thymeleaf解析器。當我只有百里酚解決方案時它工作正常,但它不是我想要的。所以我可以做什麼才能使它工作。 –

回答

0

你有幾個錯字

  • 你的項目結構的說,JSP文件夾名稱爲views/jsp下,但前綴,你剛纔提到view。所以更改爲

  • 404說/WEB-INF/view/jsp/mythymeleaf.jsp不available.Ofcourse 。它目前的/WEB-INF/views/thymeleaf/mythymeleaf.jsp

    • 這是因爲它是用InternalResourceViewResolverThymeleafViewResolver伸手JSP下。
0

我認爲,對於2個分解衝突東陽一個路徑(前綴)包含另一個(/WEB-INF/view包含/WEB-INF/view/jsp)。嘗試將* .jsp視圖移動到另一個目錄,例如/WEB-INF/jsp,像下面那樣更改internalViewResolver並且應該可以工作。

<bean id="viewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/jsp/" /> 
    <property name="suffix" value=".jsp" /> 
</bean> 

視圖的例子:

/WEB-INF/view/thymeleaf/mythymeleaf.html 
/WEB-INF/jsp/index.jsp 
/WEB-INF/jsp/myjspview.jsp 

請求映射的作品現在:

@RequestMapping("/th") 
public String th() { 
    return "thymeleaf/mythymeleaf"; 
} 

@RequestMapping("/jsp") 
public String jsp() { 
    return "index"; 
} 

@RequestMapping("/jsp2") 
public String jsp2() { 
    return "myjspview"; 
} 

看起來更好,下面的工作另配置。我已將internalViewResolver的前綴更改爲/WEB-INF/view/。看到拆分器的前綴是相同的,但ThymeleafViewResolver已將viewNames定義爲thymeleaf/*,因此所有低於/WEB-INF/view/thymeleaf/的東西都將由Thymeleaf解決。

<bean id="viewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/view/" /> 
    <property name="suffix" value=".jsp" /> 
</bean> 

沒有改變:

<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> 

瀏覽:

/WEB-INF/view/thymeleaf/mythymeleaf.html 
/WEB-INF/view/index.jsp 
/WEB-INF/view/myjspview.jsp 
相關問題