spring-mvc

pox.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.pg</groupId>
    <artifactId>cn.pg</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <!--spring核心包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <!--spring web 包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <!--spring webmvc 包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
    </dependencies>


</project>

bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">
      <bean id="catDao" class="cn.pg.dao.impl.CatDaoImpl"/>
      <bean id="catService" class="cn.pg.service.impl.CatServiceImpl">
          <property name="CatDao" ref="catDao"/>
      </bean>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <!--<context-param>-->
        <!--<param-name>beans</param-name>-->
        <!--<param-value>classpath:beans.xml</param-value>-->
    <!--</context-param>-->
    <!--配置前段-->
    <servlet>
        <servlet-name>DispatherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:beans-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:beans.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>UserServlet</servlet-name>
        <servlet-class>com.leo.web.UserServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>UserServlet</servlet-name>
        <url-pattern>/UserServlet</url-pattern>
    </servlet-mapping>
</web-app>

 

bean-mvc.xml

 

<?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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!--组件扫描-->
        <context:component-scan base-package="com.leo.controller"/>
    <!--内部资源视图解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!--配置处理器映射器-->
    <!--<bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>-->
    <!--<bean id="requestMappingHandlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">-->
        <!--<property name="messageConverters">-->
            <!--<list>-->
              <!--<ref bean="mappingJackson2HttpMessageConverter"></ref>-->
            <!--</list>-->
        <!--</property>-->
    <!--</bean>-->
    <mvc:annotation-driven/>
</beans>
package cn.pg.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/article")
public class Article {
    @RequestMapping(value = "/add2",method = RequestMethod.GET)
    public ModelAndView add2 (){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("add");
        modelAndView.addObject("username","很难");
        return modelAndView;
    }
    @RequestMapping(value = "/add1",method = RequestMethod.GET)
    public ModelAndView add1 (){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("add");
        modelAndView.addObject("username","世界很是美好");
        return modelAndView;
    }
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(){
        return "add";
    }

}
package cn.pg.dao.impl;

import cn.pg.dao.CatDao;

public class CatDaoImpl implements CatDao {
    public void say() {
        System.out.println("我是一只猫");
    }
}
package cn.pg.dao;

public interface CatDao {
    public void say();
}
package cn.pg.listener;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ContextLoaderListener implements ServletContextListener {
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        //获取全局获取参数对象
        ServletContext servletContext=servletContextEvent.getServletContext();
        //获取配置的beam.xml
        String beans=servletContext.getInitParameter("contextConfigLocation");
        //实例化context 容器
        ApplicationContext context=new ClassPathXmlApplicationContext(beans);
        //将容器保存到全局
        servletContext.setAttribute("context",context);
        System.out.println("保存ok");
    }
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
    }
}
package cn.pg.listener;

import org.springframework.context.ApplicationContext;

import javax.servlet.ServletContext;

public class WebApplicationContextUtils {
    public static ApplicationContext  getWebApplicationContext(ServletContext servletContext){
        return (ApplicationContext) servletContext.getAttribute("context");
    }
}
package cn.pg.service.impl;

import cn.pg.dao.CatDao;
import cn.pg.service.CatService;

public class CatServiceImpl implements CatService {
    private CatDao catDao;

    public CatDao getCatDao() {
        return catDao;
    }

    public void setCatDao(CatDao catDao) {
        this.catDao = catDao;
    }

    public void say() {
        this.catDao.say();
    }
}
package cn.pg.service;

public interface CatService {
    public void say();
}
package cn.pg.web;

import cn.pg.listener.WebApplicationContextUtils;
import cn.pg.service.CatService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CatServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doGet(req, resp);
        //直接获取
        //ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
        //通过监听从全局参数获取
//        ServletContext servletContext= this.getServletContext();//获取全局监听器对象
//        ApplicationContext context= (ApplicationContext) servletContext.getAttribute("context");
        //通过自定义的Utils获取
//        ServletContext servletContext= this.getServletContext();//获取全局监听器对象
//        ApplicationContext context= WebApplicationContextUtils.getWebApplicationContext(servletContext);
        //通过spring-mvc 框架的监听器获取
        ServletContext servletContext= this.getServletContext();//获取全局监听器对象
        ApplicationContext context=(ApplicationContext) org.springframework.web.context.support.WebApplicationContextUtils.getWebApplicationContext(servletContext);
        CatService catService=context.getBean("catService",CatService.class);
        catService.say();
    }

}
<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 2020/10/30
  Time: 11:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>文章</title>
</head>
<body>
    <h1>法法${username}</h1>
</body>
</html>

1414141

posted @ 2020-10-30 17:50  leo0362  阅读(137)  评论(0)    收藏  举报