spring MVC注解之入門

jopen 11年前發布 | 33K 次閱讀 Android Spring MVC Web框架

第一步: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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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">
<display-name>springmvc-chapter2</display-name>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<!-- 配置Servlet的分發以及對springMVC文件的初始路徑的引用 -->
<servlet>
    <servlet-name>chapter1</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:config/chapter1-servlet.xml</param-value>
        </init-param>

    <!-- 標識啟動容器時初始化該Servlet -->
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>chapter1</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<!-- 配置系統的編碼集合 -->
<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>utf-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

第二步:springMVC文件的配置,chapter1-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: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/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/mvc
                    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- 開啟注解,因為每次啟動都會調用下面兩個類,所以springMVC將其集成到一個配置里面,配置了
    這個,下面兩個啟用注解的類就可以不配置了 -->
    <mvc:annotation-driven />

    <!-- 此配置的作用是用來掃描所有需要注解的類,只有被掃描的類才能使用注解 -->
    <context:component-scan base-package="cn.javass.chapter2.web.controller"/>

    <!-- 啟用注解,這兩個類是使用注解時必須所配置的類,第一個作用是通過url找到類,第二個作用是通過類找到方法-->
    <!--    <bean
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
             <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean> -->

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

這個配置文件,如果一些配置報錯,那么極有可能是xml文件的頭部的那個schema驗證有問題,到網上找些正確的就OK啦!

第三步:新建一個jsp頁面,hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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>Hello World</title>
</head>
<body>
<c:forEach items="${map }" var="m">
    <c:out value="${m.value }">${m.value }</c:out>
    ${m.key }----------->${m.value }
</c:forEach>
</body>
</html>

第四步:新建一個Controller,HelloWorldController.java

package cn.javass.chapter2.web.controller;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.stereotype.Controller;

@Controller
public class HelloWorldController{
@RequestMapping(value="/hello",method=RequestMethod.GET)
public ModelAndView handleRequest(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    // 1、收集參數、驗證參數
    // 2、綁定參數到命令對象n
    // 3、將命令對象傳入業務對象進行業務處理
    // 4、選擇下一個頁面
    /*ModelAndView mv = new ModelAndView();
    // 添加模型數據 可以是任意的POJO對象
    mv.addObject("message", "This is the first test Hello World!");
    // 設置邏輯視圖名,視圖解析器會根據該名字解析到具體的視圖頁面
    mv.setViewName("hello");*/

    System.out.println("進入此方法!");
    Map<String,Object> map = new HashMap<String, Object>();
    map.put("map1", "springMVC測試1");
    map.put("map2", "springMVC測試2");
    map.put("map3", "springMVC測試3");
    return new ModelAndView("/hello","map",map);
    }
}

@Controller注解寫了之后(必須),Spring都會把它當做需要注入的Bean加載在上下文中,從而代替之前配置文件的功能;@RequestMapping(value=“/hello”,method=RequestMethod.GET),value代表訪問路徑,method代表請求方式,此處用GET請求,換成POST請求也可以。最后返回到hello.jsp視圖。

啟動tomcat,訪問http://localhost:8080/springmvc-chapter2/hello,結果如下:springMVC注解之入門

至此,一個簡單的springMVC注解就完成了!

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!