SSM整合【part2】Spring整合SpringMVC

导读:本篇文章讲解 SSM整合【part2】Spring整合SpringMVC,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

SSM整合 part2

1. 搭建SpringMVC的开发环境

1.1 在web.xml中配置DispatcherServlet前端控制器,并配置初始化参数。

contextConfigLocation参数:创建完DispatcherServlet对象,加载springmvc.xml配置文件。

load-on-startup标签:服务器启动的时候,让DispatcherServlet对象创建,参数为创建优先级。

<servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
  
<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

1.2 在web.xml中配置DispatcherServlet过滤器解决中文乱码,并在初始化参数中配置指定的字符集。

<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>
</filter>

<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

1.3 创建springmvc.xml的配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    
</beans>

1.4 配置扫描注解,且只扫描Controller的,不扫描别的。

<context:component-scan base-package="com.swz">
	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

1.5 配置视图解析器,并配置JSP文件所在的目录和文件后缀名。

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="prefix" value="/WEB-INF/pages/"/>
	<property name="suffix" value=".jsp"/>
</bean>

1.6 设置静态资源不过滤。

<mvc:resources location="/css/" mapping="/css/**" /> 
<mvc:resources location="/images/" mapping="/images/**" /> 
<mvc:resources location="/js/" mapping="/js/**" />

1.7 开启对SpringMVC注解的支持。

<mvc:annotation-driven/>

2. 单元测试

2.1 编写实现超链接跳转的 index.jsp 。

<a href="account/findAll">查询所有</a>

2.2 在WEB-INF/pages/下创建用于显示跳转后页面的 success.jsp 。

2.3 创建AccountController类。

@Controller("accountController")
@RequestMapping("account")
public class AccountController {

    @RequestMapping("findAll")
    public String testFindAll(){

        return "success";
    }
}

3. Spring整合SpringMVC的框架

整合目的:在controller中能成功的调用service对象中的方法。

思路:在项目启动的时候,就去加载applicationContext.xml的配置文件。因此可以在web.xml中配置ContextLoaderListener监听器(注:默认情况下,该监听器只能加载WEB-INF目录下的配置文件,因此需要修改路径参数)。

3.1 配置Spring的监听器,并 配置加载类路径的配置文件。

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

3.2 在controller中注入service对象。

@Controller
@RequestMapping("account")
public class AccountController {

    @Autowired
    private AccountService accoutService;

    @RequestMapping("findAll")
    public String testFindAll(){
        accoutService.findAll();
        return "success";
    }

}

3.3 启动tomcat服务器,点击index.jsp页面的超链接来调用service对象的方法进行单元测试。

以上,Spring整合SpringMVC就完成了,之后我们可以开始Spring整合Mybatis。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/84484.html

(0)

相关推荐

半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!