SpringMVC入门,集成web环境

SpringMVC入门

(新手笔记,参考自黑马程序员)

黑马程序员最全ssm框架

1、Spring集成web环境

ApplicationContext应用上下文获取方式

应用上下文对象是通过new ClasspathXmlApplicationContext(spring配置文件) 方式获取的,但是每次从容器中获得Bean时都要编写
new ClasspathXmlApplicationContext(spring配置文件) ,这样的弊端是配置文件加载多次,应用上下文对象创建多次。
在Web项目中,可以使用ServletContextListener监听Web应用的启动,我们可以在Web应用启动时,就加载Spring的配置文件,创建应用上下文对象ApplicationContext,在将其存储到最大的域servletContext域中,这样就可以在任意位置从域中获得应用上下文ApplicationContext对象了。

Spring提供获取应用上下文的工具

上面的分析不用手动实现,Spring提供了一个监听器ContextLoaderListener就是对上述功能的封装,该监听器内部加载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中,提供了一个客户端工具WebApplicationContextUtils供使用者获得应用上下文对象。
所以我们需要做的只有两件事:
① 在web.xml中配置ContextLoaderListener监听器(导入spring-web坐标)
② 使用WebApplicationContextUtils获得应用上下文对 那你象ApplicationContext

导入Spring集成web的坐标

<dependency>
 <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
   <version>5.0.5.RELEASE</version>
</dependency

配置ContextLoaderListener监听器

//web.xml
<!--全局参数,对applicationContext.xml配置文件解耦--> 
<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--Spring的监听器-->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
//listener类
public class ContextLoaderListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext servletContext = servletContextEvent.getServletContext();
        //读取web.xml的全局参数
        String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");

        //ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
        ApplicationContext app=new ClassPathXmlApplicationContext(contextConfigLocation);
        //将Spring应用上下文对象存储到ServletContext域中

        servletContext.setAttribute("app",app);
        System.out.println("spring容器创建完毕");

    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}

优化:通过工具获得应用上下文对象

ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
Object obj = applicationContext.getBean("id");

web层:

public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
        //ServletContext app = req.getServletContext();
        ServletContext servletContext = this.getServletContext();
        ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
        //上面这行代码传入的app字符串仍可以解耦,可以创建一个WebApplicationContextUtils工具类并将代码改为下一行注释内容
        // ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UserService userService = app.getBean(UserService.class);
        userService.save();

    }
}

如果自己println的提示信息出现乱码可以参考这篇博客:
IDEA 启动 tomcat 项目时控制台打印中文乱码
如果配置后日志输出又乱码了再看这篇博客:
使用tomcat启动web项目,控制台日志乱码
在这里如果上一步都改的是utf-8那么在这里也应该改成utf-8而不是gbk
(这里坑死我辽,搜了好久,靠)

2、SpringMVC概述

SpringMVC 是一种基于 Java 的实现 MVC 设计模型的请求驱动类型的轻量级 Web 框架,属于SpringFrameWork 的后续产品,已经融合在 Spring Web Flow 中。
SpringMVC 已经成为目前最主流的MVC框架之一,并且随着Spring3.0 的发布,全面超越 Struts2,成为最优秀的 MVC 框架。它通过一套注解,让一个简单的 Java 类成为处理请求的控制器,而无须实现任何接口。同时
它还支持 RESTful 编程风格的请求

3、 SpringMVC快速入门

需求:客户端发起请求,服务器端接收请求,执行逻辑并进行视图跳转。
开发步骤:
① 导入SpringMVC相关坐标
② 配置SpringMVC核心控制器DispathcerServlet
③ 创建Controller类和视图页面
④ 使用注解配置Controller类中业务方法的映射地址
⑤ 配置SpringMVC核心文件 spring-mvc.xml
⑥ 客户端发起请求测试

① 导入Spring和SpringMVC的坐标,导入Servlet和Jsp的坐标

<!--Spring坐标-->
 <dependency>
  <groupId>org.springframework</groupId
  > <artifactId>spring-context</artifactId>
  >  <version>5.0.5.RELEASE</version>
</dependency>
<!--SpringMVC坐标-->
 <dependency> 
 <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
   <version>5.0.5.RELEASE</version>
</dependency>
<!--Servlet坐标-->
 <dependency>
  <groupId>javax.servlet</groupId>
   <artifactId>servlet-api</artifactId>
    <version>2.5</version>
</dependency>
<!--Jsp坐标-->
 <dependency>
  <groupId>javax.servlet.jsp</groupId>
   <artifactId>jsp-api</artifactId>
    <version>2.0</version>
</dependency

② 在web.xml配置SpringMVC的核心控制器

<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:spring-mvc.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>

③ 创建Controller和业务方法

public class QuickController {
public String quickMethod(){
System.out.println("quickMethod running.....");
return "index.jsp";
//这里还可以写为如下:
//return "redirect:/index.jsp";
//return "forward:/index.jsp";
//REDIRECT_URL_PREFIX = "redirect:" --重定向前缀
//FORWARD_URL_PREFIX = "forward:" --转发前缀(默认值)
   }
 }

③ 创建视图页面index.jsp

<html>
 <body>
 <h2>Hello SpringMVC!</h2>
</body>
</html>

④ 配置注解
在Spring MVC 中使用 @RequestMapping 来映射请求,也就是通过它来指定控制器可以处理哪些URL请求

@Controller
public class QuickController {
@RequestMapping("/quick")
public String quickMethod(){
System.out.println("quickMethod running.....");
return "index";
    }
 }

⑤ 创建spring-mvc.xml

<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">
<!--配置注解扫描-->
 <context:component-scan base-package="com.itheima"/>
</beans>

⑥ 访问测试地址

http://localhost:8080/itheima_springmvc1/quick

测试:
在这里插入图片描述

4、SpringMVC流程图示

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值