註解 springMVC

web.xml中配置servletjava

<?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_3_0.xsd"
    version="3.0">
    <display-name>@springmvc</display-name>
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

</web-app>

spring配置文件中配置組建掃描的包和視圖解析器web

<?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:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <!-- 配置掃描組件的包 -->
    <context:component-scan base-package="com.yawn.controller"></context:component-scan>
    
    <!-- 配置視圖解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 使用InternalResourceViewResolver視圖解析器時,不須要配置viewClass屬性。默認支持JSTL -->
        <!-- <property name="viewClass" ></property> -->
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
</beans>

編寫controller控制器,並加上註解spring

package com.yawn.controller;

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

@Controller
@RequestMapping("/test")
public class TestController {
    
    @RequestMapping("/start")
    private String start() {
        System.out.println(">>>>>>>>>>>>>>>>>>-------");
        return "start";
    }
    
}

@RequestMapping("")註解能夠用在類或者方法,如上使用後,其訪問的url爲/springmvc/test/start.dospring-mvc

    返回的視圖爲:cookie

/WEB-INF/pages/test/start.jsp


@PathVariable註解:session

package com.yawn.controller;

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

@Controller
public class TestController {
    
    @RequestMapping("/start/{name}/{age}")
    private String start(@PathVariable("name") String name, @PathVariable("age") String age) {
        
        System.out.println(">>>>>>>>>>>>>>>>>>-------" + name +" , " + age);
        
        return "start";
    }
    
}

請求/start/yawn/20.do,yawn就能夠做爲參數name的值,20就能夠做爲參數age的值。
mvc


根據請求方法(get、post)的不一樣,進行不一樣的處理:app

package com.yawn.controller;

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

@Controller
public class TestController {
    
    @RequestMapping(value="/start", method=RequestMethod.GET)
    private String start() {
        
        System.out.println(">>>>>>>>>>>>>>>>>>-------get");
        
        return "start";
    }
    
    @RequestMapping(value="/start", method=RequestMethod.POST)
    private String postStart(){
        System.out.println("------------<<<<<<<<<<<<<<<<<<post");
        return "start";
    }
    
}


日期類型參數的注入和綁定:jsp

package com.yawn.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
    
    @RequestMapping(value="/start/{date}")
    private String start(@PathVariable("date") Date date) { //注入日期對象的參數的綁定
        
        System.out.println(">>>>>>>>>>>>>>>>>>-------" + date);
        
        return "start";
    }
    
    @InitBinder
    private void initBinder(WebDataBinder binder){
        
        binder.registerCustomEditor(Date.class, 
                new CustomDateEditor(new SimpleDateFormat("yyyyMMdd"), false));
    }
    
}


@RequestParam 獲得請求url中的參數:post

package com.yawn.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

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

@Controller
public class TestController {
    
    @RequestMapping(value="/start")
    private String start(@RequestParam("id") int id) {
        
        System.out.println(">>>>>>>>>>>>>>>>>>-------" + id);
        
        return "start";
    }
    
}

@CookieValue 和 @RequestHeader 獲得cookie和請求的頭信息:

package com.yawn.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

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

@Controller
public class TestController {
    
    @RequestMapping(value="/start")
    private String start(@CookieValue("userName") String userName, @RequestHeader("user-Agent") String userAgent) {
        
        System.out.println(">>>>>>>>>>>>>>>>>>-------" + userName + userAgent);
        
        return "start";
    }
    
}


處理請求的方法能夠接收的參數:

  1. request

  2. response

  3. session

  4. @RequestParam

  5. @PathVariable

  6. @CookieValue

  7. @RequestHeader

  8. 當須要輸出信息時,能夠用(PrintWriter out,Map model)

  9. 當提交登錄表單時,能夠直接使用參數(User user,BindingResult result)實體模型和綁定結果。


處理請求方法返回值的類型:

  1. void: ①使用PrintWriter輸出 ②自動從請求路徑解析返回視圖的邏輯名稱

  2. String: viewName

  3. User : 返回模型,在頁面能夠用${user.name},${user.password}取出

  4. List<User> : 在頁面能夠經過${userList}取出來

  5. Map model : 返回模型(Model類是spring對Map的實現)

  6. ModelAndView:模型和視圖(包含視圖邏輯名稱和一些鍵值對)


返回視圖時重定向到另外一個請求:

package com.yawn.controller;

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

@Controller
public class TestController {
    
    @RequestMapping(value="/start")
    private String start(@RequestHeader("user-Agent") String userAgent) {
        
        System.out.println(">>>>>>>>>>>>>>>>>>-------" + userAgent);
        
        return "redirect:/start_list";
    }
    
}
相關文章
相關標籤/搜索