userList.html
dispatcher-servlet.xml
ThymeleafeControler
web.xml
pom.xml
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- </head>
- <body>
- <div><span>用户名:</span><input name="name" value=""/><input type="button" id="search" value="查询"/></div>
- <div id="user">
- </div>
- <div id="userlist" >
- <table >
- <thead>
- <tr>
- <th>name</th>
- <th>age</th>
- <th>sex</th>
- </tr>
- </thead>
- <tr th:each="sb : ${allUsers}">
- <td th:text="${sb.name}"></td>
- <td th:text="${sb.age}"></td>
- <td th:text="${sb.sex}"></td>
- </tr>
- </table>
- <a href="thymeleafe/userList">go userList</a>
- </div>
- </body>
- </html>
dispatcher-servlet.xml
- <bean id="templateResolver"
- class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
- <property name="prefix" value="/WEB-INF/views/html/" />
- <property name="suffix" value=".html" />
- <property name="templateMode" value="HTML5" />
- <property name="cacheable" value="true" />
- <property name="characterEncoding" value="UTF-8"/>
- </bean>
- <bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
- <property name="templateResolver" ref="templateResolver" />
- </bean>
- <bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
- <property name="templateEngine" ref="templateEngine" />
- <property name="order" value="1" />
- <property name="viewNames" value="thymeleafe/*" />
- </bean>
ThymeleafeControler
- package com.cn.ld.modules.thymeleaf;
- import java.util.List;
- @RequestMapping("/thymeleafe/")
- @Controller
- public class ThymeleafeControler {
- @Autowired
- private UserService userService;
- @RequestMapping("userList")
- public String goSerchUserH(HttpServletResponse response) {
- return "userList";
- }
- @ModelAttribute("allUsers")
- public List<User> getAllUsers(){
- return this.userService.getUserList();
- }
- @RequestMapping("save")
- public void save(User user){
- this.userService.addUser(user);
- }
- @RequestMapping("remove")
- public void save(String name){
- User u = this.userService.findByName(name);
- this.userService.removeUser(u);
- }
- }
web.xml
- <!-- 字符集 过滤器 -->
- <filter>
- <filter-name>CharacterEncodingFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>CharacterEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- <dispatcher>REQUEST</dispatcher>
- <dispatcher>FORWARD</dispatcher>
- </filter-mapping>
pom.xml
- <!-- thymeleaf -->
- <dependency>
- <groupId>org.thymeleaf</groupId>
- <artifactId>thymeleaf-spring3</artifactId>
- <version>2.0.17</version>
- </dependency>
- <dependency>
- <groupId>org.thymeleaf</groupId>
- <artifactId>thymeleaf</artifactId>
- <version>2.0.17</version>
- </dependency>