ssm中需要注意的一些坑!!

谁践踏了优雅 2021-09-20 01:00 502阅读 0赞

1.index.jsp放在webcontent目录下,不要放在web-inf文件夹下面!!!

2.配置视图解析器时的“/”使用参考:

  1. <!-- 配置视图解析器 -->
  2. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  3. <property name="prefix" value="/WEB-INF/jsp/"></property>
  4. <property name="suffix" value=".jsp"></property>
  5. </bean>
  1. ${pageContext.request.contextPath } 相当于 WebContent

4.在使用标签时,记得导入 <%@ taglib uri=”http://java.sun.com/jsp/jstl/core“ prefix=”c” %> 比如:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>Insert title here</title>
  9. <script src="${pageContext.request.contextPath }/js/jquery-3.3.1.min.js"></script>
  10. <link href="${pageContext.request.contextPath }/css/bootstrap.min.css" rel="stylesheet">
  11. <script src="${pageContext.request.contextPath }/js/bootstrap.min.js"></script>
  12. </head>
  13. <body>
  14. <table class="table table-striped">
  15. <thead>
  16. <th>id<th/>
  17. <th>username<th/>
  18. <th>password<th/>
  19. </thead>
  20. <tbody>
  21. <c:forEach items="${itemList}" var="item">
  22. <tr>
  23. <td>${item.id}</td>
  24. <td>${item.username}</td>
  25. <td>${item.password}</td>
  26. </tr>
  27. </c:forEach>
  28. </tbody>
  29. </table>
  30. </body>
  31. </html>

5.有时候th和td的内容对不齐,是因为放在了下面。把直接放在下面就可以了,就对齐了。如:

  1. <thead>
  2. <th>id</th>
  3. <th>username</th>
  4. <th>password</th>
  5. </thead>

6.Could not resolve placeholder ‘jdbc.driver’ in string value “${jdbc.driver}“

  1. <context:property-placeholder location="classpath:/db.properties" **ignore-unresolvable="true"**/>

7.在连接数据库时经常出现错误,在经过一番折腾后,我总结下常见的坑。

(1)这里一定要是name=”user”,不能是name=”username”!!!血泪教训

(2)db.properties的变量名一定要一一对应,不能马虎。比如db.properties的jdbc.jdbcUrl带在value中时要写${jdbc.jdbcUrl},而不是${jdbc.Url}。我就是在这里马虎了,浪费了很多时间和精力

(3):记得将ignore-unresolvable设为true,因为默认是false

  1. <!-- 读取配置文件 数据库 -->
  2. <context:property-placeholder location="classpath:db.properties" ignore-unresolvable="true"/>
  3. <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  4. <property name="driverClass" value="${jdbc.driverClass}"></property>
  5. <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
  6. <!-- 这里一定要是name="user",不能是name="username"!!! -->
  7. <property name="user" value="${jdbc.username}"></property>
  8. <property name="password" value="${jdbc.password}"></property>
  9. </bean>

db.properties:

  1. jdbc.driverClass=com.mysql.jdbc.Driver
  2. jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm
  3. jdbc.username=root
  4. jdbc.password=root

8.mapper.xml中注意:

返回类型用:resultType,不是resultMap!!!这是个大坑!!

发表评论

表情:
评论列表 (有 0 条评论,502人围观)

还没有评论,来说两句吧...

相关阅读