表结构及数据
目录结构
相关jar包
控制器
package com.springweb.controller;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.springweb.service.Func1Service;
@Controller
@RequestMapping("/func1")
public class Func1Controller {
@Autowired
private Func1Service func1Service;
@Autowired
private MessageSource messageSource;
@ModelAttribute("lstFunc1")
private List getFunc1(@RequestParam Map map) {
System.out.println(messageSource.getMessage("fld2", null, null));
List lst = func1Service.select(map);
return lst;
}
@RequestMapping("/test")
public ModelAndView test(HttpServletRequest request, @RequestParam Map map) throws ServletException, IOException {
List lst = func1Service.select(map);
request.setAttribute("error", "func1/test");
return new ModelAndView("html/func1", "thymelst", lst);
}
@RequestMapping("/test2")
public String test(@RequestParam Map map, Model model) throws ServletException, IOException {
List lst = func1Service.select(map);
model.addAttribute("thymelst", lst);
model.addAttribute("error", "html/test2/invalid ");
return "html/func1";
}
@RequestMapping()
public String select(HttpServletRequest request, Model model,Locale local, @RequestParam Map map)
throws ServletException, IOException {
List lst = func1Service.select(map);
request.setAttribute("req", lst);
model.addAttribute("lst2Func1", lst);
return "showFunc1";
}
@RequestMapping("/select")
public ModelAndView select(@RequestParam Map map) throws ServletException, IOException {
List lst = func1Service.select(map);
return new ModelAndView("showFunc1", "req", lst);
}
}
业务层
package com.springweb.service;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.springweb.mapper.Func1Mapper;
@Service
public class Func1ServiceImpl implements Func1Service {
@Autowired
private Func1Mapper func1Mapper;
@Override
public List select(Map map) {
return func1Mapper.select(map);
}
}
数据库层
package com.springweb.mapper;
import java.util.List;
import java.util.Map;
public interface Mapper {
public int insert(Map map);
public List select(Map map);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.springweb.mapper.Func1Mapper">
<select id="select" resultType="Map" parameterType="Map">
select * from func1 where fld2 like '%${fld2}%'
</select>
</mapper>
JSP视图
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html><head>
<meta charset="UTF-8">
<title>showFunc1.jsp</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/web/jquery-1.12.4.js"></script>
</head>
<body>request: ${req} <hr>model: ${lst2Func1} <hr>
<table><tr>
<th>fld1</th>
<th>fld2</th>
<th>fld3</th>
<th>other</th> </tr>
<c:forEach items="${requestScope.lstFunc1}" var="func1" >
<tr>
<td>${func1.fld1}</td>
<td>${func1.fld2}</td>
<td>${func1.fld3}</td>
<td><a href="?'op':'update','fld1':'${func1.fld1}','fld2':'${func1.fld2}','fld3':'${func1.fld3}'">修改</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
html视图
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8">
<title>using thymeleaf </title>
<style type="text/css">
table,tr,td{
border: 1px solid black;
border-collapse: collapse;
}
th{
border: 1px solid red;
}
</style>
</head>
<body>
<h2 th:text="#{fld1}"></h2>
<font color=red th:text="${lstFunc1}"></font> <hr>
<font color=red >[[${error}]]</font> <hr>
<table> <thead> <tr>
<th >fld1 </th> <th >fld2</th> <th >fld3</th> <th >other</th> </tr> </thead>
<tbody> <tr th:each="func1 : ${thymelst}">
<td th:text="${{func1.fld1}}"></td> <td th:text="${func1.fld2}"></td>
<td th:text="${func1.fld3}"></td> <td >del update </td> </tr></tbody></table>
</body></html>
spring配置文件
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan
base-package="com.springweb">
</context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:resources location="/static/" mapping="/web/**"></mvc:resources>
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
<!-- class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
org.springframework.beans.BeanInstantiationException: Failed to instantiate
[org.thymeleaf.templateresolver.ServletContextTemplateResolver]: No default
constructor found;
nested exception is java.lang.NoSuchMethodException:
org.thymeleaf.templateresolver.ServletContextTemplateResolver.<init>() -->
<bean id="templateResolver"
class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="cacheable" value="false" />
<property name="characterEncoding" value="UTF-8" />
</bean>
<bean id="templateEngine"
class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="viewResolvers">
<list>
<!--using thymeleaf -->
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="characterEncoding" value="UTF-8" />
<property name="templateEngine" ref="templateEngine" />
<property name="viewNames" value="html/*" />
</bean>
<!-- using jsp -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
</list>
</property>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/njdb"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
<property name="initialSize" value="10"></property>
<property name="maxIdle" value="5"></property>
</bean>
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 引用数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis-config.xml 配置 <property name="configLocation" value="classpath:mybatis-config.xml">
</property> -->
<property name="mapperLocations"
value="classpath:com/springweb/mapper/*.xml">
<!-- <list> <value>com/spring/entity/Zp.xml</value> </list> -->
</property>
</bean>
<!-- 扫描代理类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.springweb.mapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="com/springweb/resources/i18n"></property>
</bean>
</beans>
属性文件
fld1=字段1
fld2=字段2
fld3=字段3
运行效果(jsp视图)
运行效果(thymeleaf)