初识springMVC

SpringMVC

spring框架提供了Web应用程序的全功能MVC模块。参考原文:http://www.cnblogs.com/bigdataZJ/p/springmvc1.html

web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    
    <!-- 配置DispatchcerServlet -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 配置Spring mvc下的配置文件的位置和名称 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
</web-app>

 

默认的配置文件格式为/WEB-INF/[servlet-name]-servlet.xml,对应这里的就是springDispatcherServlet-servlet.xml

 

在src目录下新建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"
    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-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
        
        
        <!-- 配置自动扫描的包 -->
        <context:component-scan base-package="com.abc.springmvc"></context:component-scan>
        
        <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name = "prefix" value="/WEB-INF/views/"></property>
            <property name = "suffix" value = ".jsp"></property>
        </bean>
</beans>

 

创建 HelloWorld.java(com.abc.springmvc.handlers下)

package com.abc.springmvc.handlers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloWorld {

    /**
     * 1. 使用RequestMapping注解来映射请求的URL
     * 2. 返回值会通过视图解析器解析为实际的物理视图, 对于InternalResourceViewResolver视图解析器,会做如下解析
     * 通过prefix+returnVal+suffix 这样的方式得到实际的物理视图,然后会转发操作
     * "/WEB-INF/views/success.jsp"
     * @return
     */
    @RequestMapping("/helloworld")
    public String hello(){
        System.out.println("hello world");
        return "success";
    }
}

在WebContent下创建 index.jsp 页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>helloworld</title>
</head>
<body>
<a href="helloworld">helloworld</a>
</body>
</html>

 

 当访问index.jsp时,页面上会展示一个超链接,点击超链后,url中的地址就会发生跳转,由“http://localhost:8080/springTest/index.jsp”跳转到“http://localhost:8080/springTest/helloworld”,而这个url请求就会进入HelloWorld中的hello方法,因为其与该方法上的“/helloworld”匹配。

在WEB-INF/views下创建success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>Success page</h4>
</body>
</html>

 

2. spring mvc如何访问静态资源

  关于使用spring mvc处理静态资源,比如html(发现之前的springmvc.xml中<property name = "suffix" value = ".jsp"></property>定义为jsp结尾就可以成功跳转,但是如果改为html并在web-inf下面新建了html文件后,并将suffix这里的".jsp"改为".html",无法跳转到想要的html页面,并且给出404错误,同时console给出错误信息为:No mapping found for HTTP request with URI [/springTest/WEB-INF/views/result.html] in DispatcherServ)

  其匹配的都是controller中类似@RequestMapping("/springmvc/helloworld")这样的注解配置的请求,而对于类似html/css/jpg等资源的访问就会得不到,所以需要在web.xml中加入以下类型的支持

<servlet-mapping>
     <servlet-name>default</servlet-name>
     <url-pattern>*.css</url-pattern>
</servlet-mapping>

<servlet-mapping>
      <servlet-name>default</servlet-name>
      <url-pattern>*.gif</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>default</servlet-name>
     <url-pattern>*.jpg</url-pattern>
</servlet-mapping>

<servlet-mapping>
     <servlet-name>default</servlet-name>
     <url-pattern>*.js</url-pattern>
</servlet-mapping>

<servlet-mapping>
      <servlet-name>default</servlet-name>
      <url-pattern>*.html</url-pattern>
</servlet-mapping>

 

3.spring mvc如何获取请求的参数

 

1. @PathVariable

 

  该注解用来映射请求URL中绑定的占位符。

 

 

@RequestMapping("/testPathVariable/{id}")
public String testPathVariable(@PathVariable(value="id") Integer id){
    System.out.println("testPathVariable:" + id);
    return SUCCESS;
}

 

在index.jsp中我们添加一条连接,用来触发一个请求:

 <a href="springmvc/testPathVariable/1">testPathVariable</a><br/><br/>

 

我们可以看到这里有一个超链接,点击后会进入到springmvc/testPathVariable/1对应的controller处理的方法中,那我们现在就是想获取到这个请求参数中的“1”,所以在testPathVariable方法上加入“/testPathVariable/id,id”,关于{id}的具体对应在该方法的参数中,通过@PathVariable(value="id")来声明要接收的请求参数,并通过Integer id来绑定和接收。通过该种方式,我们就可以得到前台页面请求的参数“1”。

2. @RequestParam

该注解也是用来获取请求参数. 在SpringMVCTest中添加方法

@RequestMapping(value="/testRequestParam")
public String testRequestParam(@RequestParam(value="username") String username, @RequestParam(value="age", required=false, defaultValue="0") int age){
    System.out.println("testRequestParam" + " username:" + username + " age:" +age);
    return SUCCESS;
}

在index.jsp添加超链接标签

<a href="springmvc/testRequestParam?username=jackie&age=12">testRequestParam</a><br/><br/>

点击页面上的超链接,就会匹配controller中testRequestParam方法上的RequestMapping的路径。注意在该方法中,我们通过@RequestParam这个注解声明了两个变量,用来获取请求中query所带的参数值,一个是username后的值,另一个是age后面的值。

  看到这里,你大概已经明白了@PathVariable和@RequestParam之间的一些区别了吧,对于像“springmvc/testPathVariable/1”这样的请求,我们通过@PathVariable来绑定请求的参数;而对于类似“springmvc/testRequestParam?username=jackie&age=12”这样的请求参数是以键值对出现的,我们通过@RequestParam来获取到如username或age后的具体请求值。

 4. <mvc:view-controller path="/" view-name="redirect:/admin/index"/> 重定向
即如果当前路径是/ 则重定向到/admin/index 

 

posted @ 2017-02-10 14:15  kangjie  阅读(214)  评论(0编辑  收藏  举报