您的位置:首页 > 编程语言 > Java开发

spring mvc

2016-03-10 00:00 337 查看


web.xml 配置:

<span style=
"font-size: 15px;"
><servlet>



<servlet-name>dispatcher</servlet-name>



<servlet-

class

>org.springframework.web.servlet.DispatcherServlet</servlet-

class

>



<init-param>



<description>加载/WEB-INF/spring-mvc/目录下的所有XML作为Spring MVC的配置文件</description>



<param-name>contextConfigLocation</param-name>



<param-value>/WEB-INF/spring-mvc/*.xml</param-value>



</init-param>



<load-

on

-startup>1</load-

on

-startup>


</servlet>


<servlet-mapping>



<servlet-name>dispatcher</servlet-name>



<url-pattern>*.htm</url-pattern>


</servlet-mapping>


</span>


这样,所有的.htm的请求,都会被DispatcherServlet处理;
初始化 DispatcherServlet 时,该框架在 web 应用程序WEB-INF 目录中寻找一个名为[servlet-名称]-servlet.xml的文件,并在那里定义相关的Beans,重写在全局中定义的任何Beans,像上面的web.xml中的代码,对应的是dispatcher-servlet.xml;当然也可以使用<init-param>元素,手动指定配置文件的路径;dispatcher-servlet.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:mvc="http://www.springframework.org/schema/mvc"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--

使Spring支持自动检测组件,如注解的Controller

-->

<context:component-scan base-package="com.minx.crm.web.controller"/>

<bean id="viewResolver"

class="org.springframework.web.servlet.view.InternalResourceViewResolver"

p:prefix="/WEB-INF/jsp/"

p:suffix=".jsp" />

</beans>

2.spring mvc处理方法支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void

ModelAndView

@RequestMapping("/show1")

public ModelAndView show1(HttpServletRequest request,

HttpServletResponse response) throws Exception {

ModelAndView mav = new ModelAndView("/demo2/show");

mav.addObject("account", "account -1");

return mav;

}

通过ModelAndView构造方法可以指定返回的页面名称,也可以通过setViewName()方法跳转到指定的页面 , 使用addObject()设置需要返回的值,addObject()有几个不同参数的方法,可以默认和指定返回对象的名字。 调用addObject()方法将值设置到一个名为ModelMap的类属性,ModelMap是LinkedHashMap的子类, 具体请看类。

Model 是一个接口, 其实现类为ExtendedModelMap,继承了ModelMap类。

model.addAttribute("pojo", pojo);

Map

@RequestMapping("/demo2/show")

public Map<String, String> getMap() {

Map<String, String> map = new HashMap<String, String>();

map.put("key1", "value-1");

map.put("key2", "value-2");

return map;

}

在jsp页面中可直通过${key1}获得到值, map.put()相当于request.setAttribute方法。 写例子时发现,key值包括 - . 时会有问题.

View 可以返回pdf excel等,暂时没详细了解。

String 指定返回的视图页面名称,结合设置的返回地址路径加上页面名称后缀即可访问到。

注意:如果方法声明了注解@ResponseBody ,则会直接将返回值输出到页面。 例如:

@RequestMapping(value = "/something", method = RequestMethod.GET)

@ResponseBody

public String helloWorld() {

return"Hello World";

}

上面的结果会将文本"Hello World "直接写到http响应流。

@RequestMapping("/welcome")

public String welcomeHandler() {

return"center";

}

对应的逻辑视图名为“center”,URL= prefix前缀+视图名称 +suffix后缀组成。
void 如果返回值为空,则响应的视图页面对应为访问地址

@RequestMapping("/welcome")

publicvoid welcomeHandler() {}

小结:

1.使用 String 作为请求处理方法的返回值类型是比较通用的方法,这样返回的逻辑视图名不会和请求 URL 绑定,具有很大的灵活性,而模型数据又可以通过 ModelMap 控制。 2.使用void,map,Model 时,返回对应的逻辑视图名称真实url为:prefix前缀+视图名称 +suffix后缀组成。 3.使用String,ModelAndView返回视图名称可以不受请求的url绑定,ModelAndView可以设置返回的视图名称。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: