1. 程式人生 > >springMVC入門一

springMVC入門一

public start ans mls 容器 介紹 inter pos lang

一、準備工作

eclipse使用maven搭建項目,項目名稱為HelloSpringMVC

二、搭建好的項目如下:

技術分享圖片

項目介紹:實現簡單的helloworld

三、具體代碼

controller類:HelloWorldController

package com.cn.springmvc;

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

@Controller
public class HelloWorldController { @RequestMapping("/hello") //訪問http://localhost:8080/HelloSpringMVC/hello public String hello(Model model){ model.addAttribute("greeting","hello Spring MVC"); //jsp頁面接收 ${greeting} return "helloworld"; //跳轉到 helloworld.jsp } }

jsp頁面:helloworld.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
>Spring4 MVC -HelloWorld</title> </head> <body> <h1>${greeting}</h1> </body> </html>

web項目配置:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

    <display-name>HelloSpringMVC</display-name>

    <servlet>
        <!-- 
            加載springmvc配置:
            1、這裏的servlet-name默認指向“spring-mvc-servlet.xml”
            2、手動指定:<init-param>
                
         -->
        <servlet-name>spring-mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        
        <!-- <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc-servlet.xml</param-value>
        </init-param> -->
        
        <!-- 
            <load-on-startup>:容器啟動時,加載這個servlet的順序
                取值:0,1,2,3... 數字越小,越先加載
         -->
        
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <!-- 
        url-pattern:servlet訪問地址url配置
        /* path.endsWith("/*")
        *. path.starsWith("*.")
        /  path.equals("/")
        其他    
     -->
    <servlet-mapping>
        <servlet-name>spring-mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- 加載其他的xml配置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/root-context.xml</param-value>
    </context-param>

    <!-- Spring 監聽器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

springMVC配置:spring-mvc-servlet.xml,root-context.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: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-4.1.xsd 
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.1.xsd 
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

    <!-- 自動掃描註解 -->
    <context:component-scan base-package="com.cn.springmvc"/>

    <!-- 
        處理靜態資源,如img等,這類資源不通過DispatcherServlet轉發,由容器自己處理
        <mvc:default-servlet-handler default-servlet-name="所使用的Web服務器默認使用的Servlet名稱" />
        
        對靜態資源文件的訪問
        <mvc:resources mapping="/img/**" location="/img/" />
     -->
    <mvc:default-servlet-handler />
    <!-- <mvc:resources /> -->

    
    <!-- 
        使用註解時需要配置這個,springMVC為@Controller分發請求所必須的
     -->
    <mvc:annotation-driven/>
    

    <!-- 
        視圖控制器:
            如果返回的是視圖邏輯名稱,如success,那麽視圖解析器就必須配置
            如果返回的是真實視圖名稱,如/index.jsp,那麽視圖解析器為可選配置
     -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/pages/" />  <!-- 前綴 -->
        <property name="suffix" value=".jsp" />     <!-- 後綴 -->
    </bean>
</beans>

pom.xml配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.cn</groupId>
  <artifactId>HelloSpringMVC</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>HelloSpringMVC Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <!-- Servlet Library -->
    <!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    
    <!-- Spring dependencies -->
    <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>    
    
    <!-- http://mvnrepository.com/artifact/org.springframework/spring-web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>

    <!-- http://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>
    
  </dependencies>
  <build>
    <finalName>HelloSpringMVC</finalName>
    
  </build>
</project>

四、訪問:http://localhost:8080/HelloSpringMVC/hello

技術分享圖片

  

springMVC入門一