SpringMVC层编写

web.xml

  • DispatcherServlet

    <!--DispatcherServlet-->
    <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
  • 乱码问题

    <!--乱码过滤-->
    <filter>
    <filter-name>encodingFilter</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>
    </filter>
    <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
  • Session

    <!--Session-->
    <session-config>
    <session-timeout>15</session-timeout>
    </session-config>

spring-mvc.xml

  • 注解驱动

    <!--1. 注解驱动-->
    <mvc:annotation-driven/>
  • 静态资源过滤

    <!--2. 静态资源过滤-->
    <mvc:default-servlet-handler/>
  • 扫描包

    <!--3. 扫描包:controller-->
    <context:component-scan base-package="com.kuang.controller"/>
  • 视图解析器

    <!--4. 视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
    </bean>

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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd"> <import resource="classpath:spring-dao.xml"/>
<import resource="classpath:spring-service.xml"/>
<import resource="classpath:spring-mvc.xml"/> </beans>

排错思路

问题1:bean不存在

步骤:

  1. 查看这个bean注入是否成功

  2. junit单元测试,观察代码是否可以查询出结果

  3. 前两步都没问题的话,问题不在我们的底层,是spring出了问题

  4. springMVC整合的时候没有调用到service层的bean:

    • applicationContext.xml没有注入bean

    • web.xml中,我们也绑定过配置文件!发现问题,我们配置的是spring-mvc.xml,这里确实没有service bean,所以报空指针。

问题2:http://localhost:8080/book/allBook 404

原因:applicationContext.xml没有import spring-mvc.xml

最新文章

  1. HTML5 canvas学习笔记(一)
  2. android Intent的常用flags
  3. 蓝凌表单的表体调用Javascript
  4. Windows python3.3下安装BeautifulSoup
  5. 松下蓄电池与UPS使用和维护
  6. CentOS 最小化安装后安装桌面
  7. vconfig
  8. FZU 2147 A-B Game(数学)
  9. iOS开发之UITableView及cell重用
  10. 【Alpha】——Fourth Scrum Meeting
  11. js匿名函数,闭包
  12. xcode升级10
  13. Java注解开发与应用案例
  14. 第十二节:Lambda、linq、SQL的相爱相杀(1)
  15. 自学Aruba6.2-控制器基本维护操作(web页面配置)
  16. Golang简单日志类
  17. 【Query】使用java对mysql数据库进行查询操作
  18. vijos 1096 津津的储存计划
  19. word2vec 小测试
  20. 一款手机端的jQuery图片滑块插件

热门文章

  1. java循环语句for与无限循环
  2. 图数据库HugeGraph源码解读 (1) —— 入门介绍
  3. SpringBoot整合Spring Security
  4. GaussDB连接与登出
  5. unity探索者之UGUI图片描边
  6. SpringSecurity权限管理系统实战—五、整合SpringSecurity(下)
  7. 边缘计算、区块链、5G,哪个能走的更远
  8. 笔记:HTTP协议基础
  9. Pytools1.0.0发布啦!
  10. WSGI 配置禁止反向DNS查找