SpringMVC中@Requestmapping参数配置以及返回类型配置

来源:互联网 发布:医疗器械网络销售制度 编辑:程序博客网 时间:2024/05/08 09:50

项目配置详细说明:

http://blog.csdn.net/x_iya/article/details/68059418

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">    <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list>    <servlet>        <servlet-name>springDispatcherServlet</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <!--配置DispatcherServlet的一个初始化参数:配置SpringMVC配置文件的位置和名称-->        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:springmvc.xml</param-value>        </init-param>        <!--            默认的配置文件: /WEB-INF/<servlet-name>-servlet.xml            -->        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>springDispatcherServlet</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>
springmvc.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"       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">    <!--配置自动扫描的包-->    <context:component-scan base-package="com.xiya.springmvc.handlers"/>    <!--配置视图解析器:如何把handler方法返回值解析为实际的物理视图-->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/views/"/>        <property name="suffix" value=".jsp"/>    </bean></beans>

可以使用@RequestMapping标注来将请求URL,如/appointments等,映射到整个类上或某个特定的处理器方法上。一般来说,类级别的标注负责将一个特定(或符合某种模式)的请求路径映射到一个控制器上,同时通过方法级别的标注来细化映射,即根据特定的HTTP请求方法(“GET”和“POST”方法等)、HTTP请求中是否携带特定参数等条件,将请求映射到匹配的方法上。

@Controllerpublic class Test {    private static final String PAGE = "hello";    @RequestMapping("/show")    public String show() {       return PAGE;    }}
D:\>curl http://localhost:8080/SpringMVCDemo/show
<html>
<head>
    <title>Hello</title>
</head>
<body>
    <h1>Hello World</h1>
</body>
</html>

对应的逻辑视图名为PAGE,  真实的URL= prefix前缀+PAGE +suffix后缀组成。

@Controllerpublic class Test {    private static final String PAGE = "hello";    @RequestMapping("/show")    @ResponseBody    public String show() {       return PAGE;    }}
如果方法声明了注解@ResponseBody ,则会直接将返回值输出到页面。
D:\>curl http://localhost:8080/SpringMVCDemo/show
hello

即为PAGE的值。

@Controllerpublic class Test {    @RequestMapping("/show")    public void show() {    }}
如果返回值为空,则响应的视图页面对应为访问地址

D:\>curl http://localhost:8080/SpringMVCDemo/show
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Error 404 Not Found</title>
</head>
<body><h2>HTTP ERROR 404</h2>
<p>Problem accessing /SpringMVCDemo/WEB-INF/views/show.jsp. Reason:
<pre>    Not Found</pre></p><hr><i><small>Powered by Jetty://</small></i><hr/>
</body>
</html>

@Controllerpublic class Test {    private static final String PAGE = "hello";    @RequestMapping(value = "/show", method = RequestMethod.POST)    public String show() {       return PAGE;    }}
HTTP ERROR 405Problem accessing /SpringMVCDemo/show. Reason:    Request method 'GET' not supportedPowered by Jetty://
D:\>curl -d "" http://localhost:8080/SpringMVCDemo/show
<html>
<head>
    <title>Hello</title>
</head>
<body>
    <h1>Hello World</h1>
</body>
</html>


Model的使用:

public interface Model {Model addAttribute(String attributeName, Object attributeValue);Model addAttribute(Object attributeValue);Model addAllAttributes(Collection<?> attributeValues);Model addAllAttributes(Map<String, ?> attributes);Model mergeAttributes(Map<String, ?> attributes);boolean containsAttribute(String attributeName);Map<String, Object> asMap();}因为Model十个接口类型,所以不能在方法中实例化。

@Controllerpublic class Test {    private static final String PAGE = "hello";    @RequestMapping(value = "/show")    public String show(Model model) {        model.addAttribute("time", new Date());       return PAGE;    }}

hello.jsp

<html><head>    <title>Hello</title></head><body>    <h1>Hello World</h1>    <h1>${time}</h1></body></html>
Map的使用:

可以用作参数或者在方法体内创建实例。

@Controllerpublic class Test {    private static final String PAGE = "hello";    @RequestMapping(value = "/show")    public String show(Map<String, Object> map) {        map.put("time", new Date());       return PAGE;    }}
在jsp页面中可直通过${key1}获得到值, map.put()相当于request.setAttribute方法。

ModelAndMap的使用:

@Controllerpublic class Test {    private static final String PAGE = "hello";    @RequestMapping(value = "/show")    public ModelAndView show() {        ModelAndView modelAndView = new ModelAndView(PAGE);        modelAndView.addObject("time", new Date());       return modelAndView;    }}
通过ModelAndView构造方法可以指定返回的页面名称,也可以通过setViewName()方法跳转到指定的页面 ,
使用addObject()设置需要返回的值,addObject()有几个不同参数的方法,可以默认和指定返回对象的名字。
调用addObject()方法将值设置到一个名为ModelMap的类属性,ModelMap是LinkedHashMap的子类,
具体请看类。

参考:https://my.oschina.net/kolbe/blog/509810

0 0
原创粉丝点击