描述:使用兩個視圖解析器,jsp和thymeleaf,項目使用Maven,Spring4html
配置方式:java
一、首先在maven中引用以下jar包:web
<!-- thymeleaf模版 --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>3.0.3.RELEASE</version> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring4</artifactId> <version>3.0.3.RELEASE</version> </dependency>
二、在配置文件spring-*.xml中配置(個人是spring-mybatis.xml)spring
<!-- JSP 視圖解析器 --> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name="viewNames" value="jsp*"></property> <property name="prefix" value="/WEB-INF/" /> <property name="suffix" value=".jsp" /> <property name="order" value="1" /> </bean> <!-- thymeleaf 視圖解析器 --> <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver"> <property name="templateEngine" ref="templateEngine" /> <property name="viewNames" value="html*"></property> <property name="order" value="0" /> </bean> <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine"> <property name="templateResolver" ref="templateResolver" /> </bean> <bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver"> <property name="prefix" value="/frontPage/" /> <property name="suffix" value=".html" /> <property name="templateMode" value="HTML5" /> </bean>
三、Controller編寫
(1) 使用 thymeleafapi
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/api") public class TestController { @RequestMapping("/test") public String getBooks(Model model){ model.addAttribute("message", "hello"); return "html/index"; } }
(2)使用 jsp
mybatis
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/login") public class LoginController { @RequestMapping("/loginManagement") public String login(){ return "jsp/login/login"; } }
四、在 html 文件中要引入這個網址app
xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <table> <tbody> <tr> <td th:text="${message}">Red Chair</td> </tr> </tbody> </table> </body> </html>
五、每一個文件在項目中的位置
jsp