Spring MVC

一.springMVC步骤

1.导入jar

 

2.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" 

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_3_0.xsd">

  <servlet>

      <servlet-name>springDispatcherServlet</servlet-name>

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

      <init-param>

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

         <param-value>classpath:spring-mvc.xml</param-value>

      </init-param>

  </servlet>

  <servlet-mapping>

     <servlet-name>springDispatcherServlet</servlet-name>

     <url-pattern>/</url-pattern>

  </servlet-mapping>

</web-app>

3.编写spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"  

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

    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-4.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-4.0.xsd">  

    

    <context:component-scan base-package="com.bdqn"/>

    <!-- 视图解析 器 -->

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  

        <property name="prefix" value="/"/>  

        <property name="suffix" value=".jsp"/>  

    </bean>  

    </beans>

4.编写Controller类

package com.bdqn.Controller;

import org.springframework.stereotype.Controller;

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

@Controller

public class HelloWorld {

@RequestMapping("/helloworld")

public String sss(){

System.out.println("helloWorld");

return "success";

}

}

5.编写页面.jsp

<body>

<a href="helloworld">helloworld</a>

  </body>

二.@RequestMapping注解

@RequestMaping注解不但可以注解方法还可以注解类

@RequestMapping(values=””,method=”post”)

可以使用paramsheaders来更加精确映射条件,paramsheaders

@RequestMapping(values=””,param=”{}”,headers=”{}”);

三.@PathVariable映射URL中的占位符到目标方法中的参数

 

四.REST

 

web.xml里面配置org.springframwork.web.filter.hiddenHttpMethodFilter可以把post请求转换为其他的

 

五.@RequestParam来映射请求参数

 

 

 

六.ModelAndView

MapModelMap处理模型数据

@RequestMapping(“/testMap”)

Public String testMap(Map<String,object> map){

  Map.put(“names”,array.aslist(“tome”));

  System.out.println(map.);

}

七.处理模型数据之SessionAttributes()

@RequestMapping(“/testSessionAttributes”)

Public String testSessionAttributes(Map<String,object> map){

User user=new User();

Map.put(“user”,”user”);

  Return success;

}

 

 

八.ModelAttribute注解之使用的场景

 

posted @ 2015-09-11 12:39  汤苗苗  阅读(495)  评论(0编辑  收藏  举报