2016-10-06 18 views
-1

私はthymeleafの初心者です。私はspring mvcプロジェクトでjspとthymeleafの両方を使いたいと思っています。設定はすべて完璧なようですが、404例外を投げていますmsgstr "リクエストリソースは利用できません" msgid。助けてください。同じspring mvcプロジェクトのjspとthymeleafの使い方

404 exception!

そして、私の設定このように書きます:

春サーブレット-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-config設定

<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

ご回答のSAMてくれてありがとうとmariooshを私はあなたの提案どおりに働いてきたが、まだそれはthymeleafで作業されていません。 @samはJSPビューのリゾルバには到達していますが、Thymeleafリゾルバには到達していません。私はthymeresolverしか持っていませんが、うまくいきますが、私が望むものではありません。 –

答えて

0

あなたがタイプミスのカップル

  • プロジェクト構造を有しているが、JSPのフォルダ名はviews/jspの下にあるが、接頭辞であなただけのviewを言及していると言います。だから、available.Ofcourseされていない

  • 404言う/WEB-INF/view/jsp/mythymeleaf.jspに変更しはい/WEB-INF/views/thymeleaf/mythymeleaf.jsp

    • それはInternalResourceViewResolverないThymeleafViewResolverを使用しているので、それをJSPに達しています下の .ITの存在。
0

私は1 beacause 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/に変更しました。 reseolverのプレフィックスは同じですが、ThymeleafViewResolverviewNamesthymeleaf/*と定義されているので、/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 
関連する問題