springMVC系列之参数、多方法、配置文件加载——02
来源:互联网 发布:java语言入门培训班 编辑:程序博客网 时间:2024/05/21 17:52
springMVC系列之参数、多方法、配置文件加载——02
摘要:本文是在前者的基础上实现修改配置文件springMVC-servlet.xml的默认位置及名称、后台向前台传递参数的方式、以及在一个controller中可选择性的执行不同的方法。
一:实现功能
1、配置启动时加载的配置文件的路径
2、传递参数的方式String、map、使用jstl来获取map
3、在controller中同时实现写多个方法
二:具体实现步骤
1、配置启动时加载的配置文件的路径
a)既然要在启动时加载指定位置的、指定名称的spring配置文件、则很容易联想到在 哪里配置: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处理--><servlet><servlet-name>springMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><!-- 下方classpath后面有个*号是表示会加载所有在classpath路径下的后面指定的文件、不配置的话只会加载一个。在大型项目开发中、一般都是以模块来开发、各个小组都有自己的配置文件、这样既具有独立性又容易管理。 --><param-value>classpath*:config/spring-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springMVC</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>
b)当然是在你指定的配置文件存放的目录下创建spring的配置文件:这里放在src下新建的config目录下、名称是web.xml中指定的spring-servlet.xml。
2、传递参数的方式String、map、使用jstl来获取map
a)HelloWorldController中有直接的说明,源码:
package com.chy.web.controller;import java.util.HashMap;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;public class HelloWorldController implements Controller{public ModelAndView handleRequest(HttpServletRequest arg0,HttpServletResponse arg1) throws Exception {System.out.println("hello dear mr.chen !");@SuppressWarnings("unused")String hello = "hello";Map<String,Object> map = new HashMap<String, Object>();map.put("map1", "chy1");map.put("map2", "chy2");map.put("map3", "chy3");/* * 原理是返回的ModelAndView的构造方法支持 new ModelAndView("返回的jsp名称", "key", Value) * 其中的Value可以是Object、 *///传递String//return new ModelAndView("welcome","hello",hello);//传递mapreturn new ModelAndView("welcome","map",map);}}
b)接收的页面:是HelloWorldController中返回的第一个参数指定的jsp名称: welcome.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'welcome.jsp' starting page</title> </head> <body> This is my first springMVC page ! <h1>传递信息</h1> ${requestScope.result} <hr/> ${requestScope.map.map1}<br/> ${requestScope.map.map2}<br/> ${requestScope.map.map3}<br/> 使用jstl获取key,value<br/> <c:forEach items="${requestScope.map}" var="m"> ${m.key} +++ ${m.value}<br/> </c:forEach> </body></html>
3、在controller中同时实现写多个方法
a)首先配置一个方法解析器、即指定什么样的请求后缀会被认为是方法、
b)配置一个bean指定处理请求的controller、这个bean有个解析方法的属性、ref到上面配置的方法解析器的bean
spring-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.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-3.0.xsd"> <!-- 配置方法解析器 --> <bean id="paramMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"><property name="paramName" value="action"></property></bean><!-- 将方法解析器引入到此controller中 --> <bean name="/test1/multi" class="com.chy.web.controller.MultipleController"><property name="methodNameResolver"><ref bean="paramMethodResolver"/></property></bean><bean name="/test1/helloworld" class="com.chy.web.controller.HelloWorldController" /><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean> </beans>
c)测试的controller——MultipleController:
package com.chy.web.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.multiaction.MultiActionController;public class MultipleController extends MultiActionController{public ModelAndView add(HttpServletRequest request, HttpServletResponse response){String method = "add method";return new ModelAndView("multiple", "method", method);}public ModelAndView update(HttpServletRequest request, HttpServletResponse response){String method = "update method";return new ModelAndView("multiple", "method", method);}}
d)注意浏览器中url的输入!http://localhost:8080/springMVC2/test1/multi?action=add
/test1/multi是配置中指定的请求方式、action是方法解析器中配置的方法后缀名、add是具体controller中的处理此请求的方法。
补充:
更多内容: springMVC系列之目录——00
- springMVC系列之参数、多方法、配置文件加载——02
- springMVC系列源码之配置文件深入——11
- springmvc 多方法访问
- SpringMVC多方法控制器
- Spring mvc系列二之 控制器多方法访问
- Spring mvc系列二之 控制器多方法访问
- springMVC系列(八)——springMVC参数接收详解
- springmvc系列(二)-自定义dispatcherservlet加载的xml配置文件
- springMVC笔记系列(18)——配置文件细节详解
- springMVC系列之目录——00
- springMVC系列之前台参数的传递——05
- springmvc笔记之 -- 配置文件
- apache kafka系列之server.properties配置文件参数说明
- apache kafka系列之server.properties配置文件参数说明
- apache kafka系列之server.properties配置文件参数说明
- apache kafka系列之server.properties配置文件参数说明
- apache kafka系列之server.properties配置文件参数说明
- apache kafka系列之server.properties配置文件参数说明
- ios-arc用法
- 使用redis作为php中session的存储介质
- brightcove视频流下载
- A Test in MASM and GCC 64-bits for instruction mov and lea
- DOJO 基本原理 之 dojo/_base/declare<7>
- springMVC系列之参数、多方法、配置文件加载——02
- oracle导入导出工具exp/imp
- NYOJ 261 玩转矩阵的C小加
- 触发、清除 haslayout的css属性
- UITabBarController 简单例子
- CSS选择符
- 帧边界识别简介
- ORACLE事物隔离级别
- Mongodb java代码