1. 程式人生 > >SpringMVC最簡單配置應用

SpringMVC最簡單配置應用

一、專案配置

1.建立java web專案

2.匯入相關jar包

3.配置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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringMVC</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 配置dispatcherServlet -->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:springmvc_servlet.xml</param-value>
    </init-param>
  </servlet>
  
  <servlet-mapping>
     <servlet-name>springmvc</servlet-name>
     <url-pattern>/</url-pattern>
  </servlet-mapping>
  
 <!-- 編碼過濾器 -->
 <filter>
   <filter-name>encode</filter-name>
   <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
   <async-supported>true</async-supported> <!-- 非同步 --> 
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
 </filter>
 
 <filter-mapping>
   <filter-name>encode</filter-name>
   <url-pattern>/*</url-pattern>
 </filter-mapping>
  
  
</web-app>

4.建立springmvc_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"  
    xmlns:util="http://www.springframework.org/schema/util"  
    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  
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> 

      
  <!-- 使用註解方式完成對映 -->
  <!-- 讓掃描spring掃描這個包下所有的類,讓標註spring註解的類生效 -->
  <context:component-scan base-package="com.etc.controller"/>      
   <mvc:annotation-driven/><!-- 開啟註解 -->
   
    <!-- 檢視解析器 -->    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"></property>
    </bean>     
</beans>

5.建立Controller

建包com.etc.controller

建類檔案HelloController.java

package com.etc.controller;

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

@Controller
public class HelloController {
    @RequestMapping(value="/welcome")  //welcome要訪問的url地址
    public  String  hello(){
        System.out.println("hello,springmvc");
        return "hello";  //hello是邏輯檢視名,和字尾名組合一起構成檢視名  /web-inf/jsp/hello.jsp
    }

}

6.建立檢視

在WEB-INF下建立jsp資料夾,在檔案下建立hello.jsp檔案

7.在瀏覽器輸入訪問地址

http://localhost:8080/SpringMVC/welcome

二、引數傳遞

1.前臺到後臺

方法一 http://localhost:8080/SpringMVC/welcome?uname="333"

@RequestMapping(value="/welcome")  //welcome要訪問的url地址
    public  String  hello(String uname){//此時方法引數與傳來引數名稱一致
        System.out.println("hello,springmvc"+uname);
        return "hello";  //hello是邏輯檢視名,和字尾名組合一起構成檢視名  /web-inf/jsp/hello.jsp
    }

方法二  http://localhost:8080/SpringMVC/welcome2?username="333"

@RequestMapping(value="/welcome2")  //welcome要訪問的url地址
    public  String  hello2(@RequestParam(value="username",required=false)String uname){
        //此時方法引數value="username"與傳來引數名稱一致,required=false不傳參uname為null
        //required=false不傳參出錯
        System.out.println("hello,springmvc"+uname);
        return "hello";  //hello是邏輯檢視名,和字尾名組合一起構成檢視名  /web-inf/jsp/hello.jsp
    }

2.後臺向前臺傳參

方法一    http://localhost:8080/SpringMVC/welcome3?uname="333"

@RequestMapping(value="/welcome3")  
    public  String hello3(String  uname,Model model){
        System.out.println("頁面過來的引數是:"+uname);
        model.addAttribute("username","張三");
        return "hello";
    }

前臺獲取

<body>
    後臺傳遞的引數:${username} <br>
  </body>

方法二 http://localhost:8080/SpringMVC/welcome4?uname="333"

@RequestMapping(value="/welcome4")  
    public  String hello4(String  uname,Map<String,Object> map){
        System.out.println("頁面過來的引數是:"+uname);
        map.put("username","張三");
        return "hello";
    }

前臺同上

方法三 http://localhost:8080/SpringMVC/welcome4?uname="333"

@RequestMapping(value="/welcome5")  
    public  ModelAndView hello5(String uname){
        ModelAndView mv=new ModelAndView();
        Users user=new Users();
        user.setId(1);
        user.setName("李紅");
        mv.addObject("username",uname);
        mv.addObject(uname);
        mv.addObject("user", user);
        
        mv.setViewName("hello");
        return mv;

    }

前臺

<body>
    後臺傳遞的引數:${username} <br>
    ${string}<br>
    ${user.name }
  </body>