繁体   English   中英

Spring MVC集成Thymeleaf

[英]Spring MVC integration Thymeleaf

我有个问题。 我创建了Spring MVC + Thymeleft项目,但是无法正确集成,我阅读了许多指南和文档,但没有任何帮助。 什么问题? 页面加载,没有错误,但是没有相关信息。

spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
      destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/practick" />
    <property name="username" value="root" />
    <property name="password" value="" />
</bean>

<!-- Hibernate 3 Annotation SessionFactory Bean definition-->
<bean id="hibernate3AnnotatedSessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>SpringMVC1.model.Store</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.current_session_context_class">thread</prop>
            <prop key="hibernate.enable_lazy_load_no_trans">false</prop>
            <prop key="hibernate.show_sql">false</prop>
        </props>
    </property>
</bean>

<mvc:resources mapping="/SpringMVC1/**" location="/WEB-INF/views" />

<!-- class beans -->
<bean id="storeDAO" class="SpringMVC1.dao.Impl.StoreDAOImpl">
    <property name="sessionFactory" ref="hibernate3AnnotatedSessionFactory" />
</bean>

<bean id="storeModel" class="SpringMVC1.model.Store">
</bean>

<!-- Thymeleaf -->
<bean id="templateResolver"
      class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
    <property name="prefix" value="/views/" />
    <property name="suffix" value=".jsp" />
    <property name="templateMode" value="HTML5" />
</bean>

<bean id="templateEngine"
      class="org.thymeleaf.spring3.SpringTemplateEngine">
    <property name="templateResolver" ref="templateResolver" />
    <property name="additionalDialects">
        <set>
            <bean class = "org.thymeleaf.spring3.dialect.SpringStandardDialect"/>
        </set>
    </property>
</bean>

<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
    <property name="templateEngine" ref="templateEngine" />
    <property name="order" value="1" />
    <property name="viewNames" value="*.html,*.xhtml,*.jsp" />
    <property name="excludedViewNames">
        <array>
            <value>home.jsp</value>
        </array>
    </property>
</bean>

</beans>

pom.xml中插入依赖项:

<!-- Thymeleaf -->
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf</artifactId>
        <version>2.1.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring3</artifactId>
        <version>2.1.1.RELEASE</version>
    </dependency>

控制器原型

@Controller
public class HomeController {

@Autowired
private ServletContext servletContext;

TemplateEngine templateEngine;

private static Store store;
private static StoreDAOImpl storeDAOImpl;
static List<Store> storess;

static{
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    storeDAOImpl = (StoreDAOImpl)context.getBean("storeDAO");
    store = (Store)context.getBean("storeModel");
}

/*
@RequestMapping(value="/home")
public ModelAndView handleRequest(HttpServletRequest req,
                                  HttpServletResponse resp) throws Exception {

    ModelAndView modelAndView = new ModelAndView("home");
    //modelAndView.addObject("storeList", storeDAO.getAllStores());

    return modelAndView;
}

    @RequestMapping(value = "/storeModel")
    public ModelAndView root(@PathVariable String appBeanId, Locale locale,
                       org.springframework.ui.Model model) {
        appBeanId="storeModel";
        locale.getCountry();
        store.setName("store1");
        model.addAttribute("store1", store);
        return new ModelAndView("home");
    }
*/
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

@RequestMapping(value = "/home")
public String login(@Validated Store store, Model model) {
    model.addAttribute("storeName", "sometext");
    return "home";
}


/*
//public static List stores = storeDAOImpl.getAllStores();

@RequestMapping(value = "/listStores")
public ModelAndView y(HttpServletRequest req,
                      HttpServletResponse resp) throws SQLException, IOException {
    store.setName("store1");
    WebContext webContext = new WebContext(req, resp, servletContext);
    webContext.setVariable("stores", storeDAOImpl.getAllStores());
    webContext.setVariable("store1", store);
    return new ModelAndView("home");
}
*/
}

home.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Home</title>
</head>
<body>
    <h1>Hello World!</h1>
    <p>This is the stores!</p>
 <!--       <c:forEach items="${storeList}" var="store">
        ${store.name} ${store.type}: ${store.address}
        <br />
    </c:forEach>
 -->
<table>
    <caption> Store #2</caption>
    <tr></tr>
        <td><span th:text = "${storeName}">0</span></td>
</table>

</body>
</html>

卷筒纸项目结构

如我所知,要从控制器返回jsp页面,您应该使用smth:

return new ModelAndView("home.jsp")

这是关于返回jsp的问题

如果String像您一样返回,则它用于返回存储在classpath中的html页面。 在百里香中通常称为模板

以及为什么非盟试图同时使用百里香和jsp。 我可能是错的,但这是两种竞争技术。

在百里香官方网站上 :“ Thymeleaf提供了一组Spring集成,使您可以将其用作Spring MVC应用程序中JSP的功能全面的替代品。”

我在一个公共github 项目中发布了一个有效的(经过码头测试)解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM