SpringMVC01

1.创建一个web项目  引入所需要的jar

2.在web.xml文件中配置 核心控制器

web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
    <!--  配置 springmvc的核心控制器    
     这里的 <servlet-name>springmvc</servlet-name>
     springmvc就是规定了 我们springmvc的配置文件的名称
     <servlet-name>-servlet.xml
      -->
     <servlet> 
     <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     </servlet>
     
     <servlet-mapping> 
     <servlet-name>springmvc</servlet-name>
     <url-pattern>/</url-pattern> 
     <!--  为什么不能配置/*
      我们打开浏览器 第一次访问  项目名/hello ,会通过我们的handlerMaping找 有没有bean name="/hello"!
      有就进入对应的controller进行操作,之后根据controller中返回的ModelAndView ===》hello01!
     让试图解析器进行解析 :解析之后 变成了/SpringMVC001/WEB-INF/jsp/hello01.jsp
     
     如果我们配置的是 /*  那么解析之后的路径也会被当成一个Bean  name来处理,很显然我们没有这个bean,所以会报错!
     
     /: 只会匹配  /login    /index   不能匹配  /*.jsp 这样的后缀url
     /*:匹配所有的请求 所以我们hello01.jsp 也会被拦截
       -->
     </servlet-mapping>
    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

web.xml文件

3.在web-inf目录下创建springmvc的核心配置文件

springmvc-servlet.xml核心配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    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/tx 
http://www.springframework.org/schema/tx/spring-tx.xsd 
http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--  采用默认的方式  BeanNameUrlHandlerMapping -->
 <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
 
 <!-- name="/hello" 用户的请求   class就是我们的Controller处理器-->
 <bean name="/hello" class="cn.bdqn.controller.HelloController"/>

<!-- 视图解析器         项目名/web-inf/jsp/springmvc.jsp  需要设置前缀 和后缀 -->   
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>

</bean>

</beans>

springmvc-servlet.xml核心配置文件

4.创建对应的controller文件

HelloController代码

public class HelloController extends AbstractController { // 第一个springmvc小例子
    /**
     * handleRequestInternal():对应struts2的 execute()
     * ModelAndView:返回值   需要核心配置文件中的试图解析器 解析
     */
    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        System.out.println("第一个springmvc小例子....");
        // hello01就是我们返回的视图名称 但是 现在只是一个字符串
        return new ModelAndView("hello01");
    }
}

HelloController代码

5.在web-inf下新建jsp文件夹,之后再jsp文件夹下面新建hello01.jsp页面即可

6.向前台页面传值

HelloController中增加代码

public class HelloController extends AbstractController {// 可以看出来
                                                            // 这一个controller中只能有一个方法

    /**
     * 03.向前台传递一个Map集合
     * 
     */
    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        System.out.println("向前台传递一个Map集合");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("1", "大家辛苦1");
        map.put("2", "大家辛苦2");
        map.put("3", "大家辛苦3");
        return new ModelAndView("hello03", "map", map);
    }

    /**
     * 02.向前台界面传递值
    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        System.out.println("进入了向前台界面传递值的方法..... ");
        // request.setAttribute("result", "数据是从后来获取的!");
        // 我在返回视图的时候 携带数据
        return new ModelAndView("hello02", "result", "数据是从后来获取的!");
    }
    */
    /** 01.
     * handleRequestInternal():对应struts2的 execute()
     * ModelAndView:返回值   需要核心配置文件中的试图解析器 解析
     
    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        System.out.println("第一个springmvc小例子....");
        // hello01就是我们返回的视图名称 但是 现在只是一个字符串
        return new ModelAndView("hello01");
    }
    */
}

HelloController中增加代码

7.在jsp文件夹下面新增需要的两个界面  其余代码不动

hello02.jsp

<body>
  获取后台controller中的result值:${result}  
  </body>

hello03.jsp

<%--引入jstl标签库--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<body>

    获取后台controller中的map集合的值: 单独的获取一个值=====》${map["1"]}
    <br />
    <%-- 使用循环 遍历 map集合   --%>
    <c:forEach items="${map}" var="m">
      ${m.key}=======>${m.value}<br />
    </c:forEach>

</body>

=================修改核心配置文件的位置和名称======================= 

 只需要修改web.xml中的servlet节点

<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:/springmvc-servlet.xml</param-value>
     </init-param>
     </servlet>
     
     <servlet-mapping> 
     <servlet-name>springmvc</servlet-name>
     <url-pattern>/</url-pattern> 

然后把核心配置文件放到src下面即可!

 

 

原文地址:https://www.cnblogs.com/xtdxs/p/6593668.html