如何在同一个春季mvc项目中使用jsp和thymeleaf

时间:2016-10-06 04:04:11

标签: jsp spring-mvc thymeleaf

我是thymeleaf的新手。我想在我的spring mvc项目中同时使用jsp和thymeleaf,一切看起来都很完美,但由于"请求资源不可用而导致404例外。#34; 。请帮忙。

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

2 个答案:

答案 0 :(得分:0)

你有几个拼写错误

  • 您的项目结构显示jsp文件夹名称位于views/jsp下,但在前缀中您只提到了view。所以改为

  • 404说/WEB-INF/view/jsp/mythymeleaf.jsp不可用.Ofcourse 。它出现在/WEB-INF/views/thymeleaf/mythymeleaf.jsp

    • 它正在使用JSP,因为它使用InternalResourceViewResolver而不是ThymeleafViewResolver

答案 1 :(得分:0)

我认为两个解析器的路径(前缀)冲突,因为其中一个包含另一个(/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/。请注意,重新定位器的前缀相同,但ThymeleafViewResolverviewNames定义为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