1. 程式人生 > >springmvc快速入門(註解版本)

springmvc快速入門(註解版本)

1)springmvc快速入門(傳統版)

   步一:建立springmvc-day02這麼一個web應用

   步二:匯入springioc,springweb和springmvc相關的jar包

  ------------------------------------------------------springWEB模組
   org.springframework.web-3.0.5.RELEASE.jar
org.springframework.web.servlet-3.0.5.RELEASE.jar(mvc專用)
   ------------------------------------------------------springIOC模組
   org.springframework.asm-3.0.5.RELEASE.jar
   org.springframework.beans-3.0.5.RELEASE.jar
   org.springframework.context-3.0.5.RELEASE.jar
   org.springframework.core-3.0.5.RELEASE.jar
   org.springframework.expression-3.0.5.RELEASE.jar

步三:在/WEB-INF/下建立web.xml檔案

<servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</
param-name> <param-value>classpath:spring.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping
>

步四:建立HelloAction.java控制器類

@Controller
public class HelloAction{
    @RequestMapping(value="/hello")
    public String helloMethod(Model model) throws Exception{
        System.out.println("HelloAction::helloMethod()");
        model.addAttribute("message","這是我的第二個springmvc應用程式");
        return "/success.jsp";
    }    
}

步五:在/WebRoot/下建立success.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>這是我的第二個springmvc應用程式</title>
  </head>
  <body>
    success.jsp<br/>
    ${message}
  </body>
</html>

步六:在/src/目錄下建立spring.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:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
        
      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
        
      ">
        

      <!-- Action控制器 -->
      <context:component-scan base-package="loaderman.javaee.springmvc.helloannotation"/>      

      
      
      <!-- 基於註解的對映器(可選) -->
      <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
      
      <!-- 基於註解的介面卡(可選) -->
      <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
      
      <!-- 檢視解析器(可選) -->
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
          
</beans>

步七:部署web應用到tomcat中,通過瀏覽器訪問hello.action的URL