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不存在
步骤:
-
查看这个bean注入是否成功
-
前两步都没问题的话,问题不在我们的底层,是spring出了问题
-
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。