springMVC修改配置文件路径与给页面传递数据

在上一节,说道springMVC的配置文件,但是,配置文件是SpringMVC-servlet.xml,并不能进行更改。这样有一些死板,并且在项目中通常会有多个人同时进行项目的开发。

这样就不能只有一个配置文件。

首先,web.xml文件中基础配置有springMVC配置的servlet路径

<servlet-name>SpringMVC</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

同时还有,配置加载顺序的:

<load-on-startup>1</load-on-startup>

我们如果需要加载其他地方的多个springMVC配置文件,就需要在代码中加入如下代码:

   <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:config/*-servlet.xml</param-value>
   </init-param>

需要注意的是:加入的代码必须放在<servlet-class>...</servlet-class>与<load-on-startup>...</load-on-startup>之间。

其中<param-name>contextConfigLocation</param-name>为参数的配置,所需要的参数可以在org.springframework.web.servlet.DispatcherServlet中得到。

其部分源码如下:


 /**
  * Create a new {@code DispatcherServlet} that will create its own internal web
  * application context based on defaults and values provided through servlet
  * init-params. Typically used in Servlet 2.5 or earlier environments, where the only
  * option for servlet registration is through {@code web.xml} which requires the use
  * of a no-arg constructor.
  * <p>Calling {@link #setContextConfigLocation} (init-param 'contextConfigLocation')
  * will dictate which XML files will be loaded by the
  * {@linkplain #DEFAULT_CONTEXT_CLASS default XmlWebApplicationContext}
  * <p>Calling {@link #setContextClass} (init-param 'contextClass') overrides the
  * default {@code XmlWebApplicationContext} and allows for specifying an alternative class,
  * such as {@code AnnotationConfigWebApplicationContext}.
  * <p>Calling {@link #setContextInitializerClasses} (init-param 'contextInitializerClasses')
  * indicates which {@code ApplicationContextInitializer} classes should be used to
  * further configure the internal application context prior to refresh().
  * @see #DispatcherServlet(WebApplicationContext)
  */
 public DispatcherServlet() {
  super();
 }
红色部分即为配置的参数。

最后得到的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">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <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*:config/*-servlet.xml</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup> <!-- 启动tomcat时启动springMFC -->
  </servlet>
  <servlet-mapping>
   <servlet-name>SpringMVC</servlet-name>
   <url-pattern>/</url-pattern><!-- 拦截所有请求 -->
  </servlet-mapping>
</web-app>
其中classpath*:config/*-servlet.xml的意思解释如下:

classpath*代表在src下寻找config文件夹再在其中寻找以-servlet.xml文件结尾的文件。

这样就可以达到多配置文件的目的。

 

 

下面是数据的传输:

在springMVC的配置文件中,已经配置了<bean>如下:

<bean name="/test/hello2" class="com.yx.controller.HelloSpringMVCController"></bean>

在配置的bean的java文件中的handleRequest方法。这一步只是为了构造数据:

System.out.println("-------进入HelloSpringMVCController类--------");
  String hello = "测试:spring数据从后台传入页面";
  Map<String,Object> map = new HashMap<String,Object>();
  map.put("map1", "第一个map");
  map.put("map2", "第二个map");
  map.put("map3", "第三个map");
  map.put("map4", "第四个map");
  //return new ModelAndView("/hello","StringResult",hello);//返回String类型的数据到页面
  return new ModelAndView("/hello","mapResult",map);//返回map类型的数据到页面

在写jsp页面之前需要导入两个jar文件,分别为jstl.jar与standard.jar

这两个是为了方便显示的。

在hello.jsp页面中首先需要导入jar包

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

然后加入如下代码

 你好SpringMVC!!!<br/>
    <div>
     <h3>后台数据显示:</h3>
     <!-- 获得String类型的数据 -->
     <!--  ${StringResult }-->
     <!-- 获得map类型的数据 -->
     <c:forEach items="${mapResult}" var="mapVar">
      ${mapVar.key }----------->${mapVar.value }<br/>
     
     </c:forEach>
    </div>

这样就可以将后台的数据显示在页面上了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值