1. 程式人生 > >springmvc-servlet.xml檔案配置,方法一

springmvc-servlet.xml檔案配置,方法一

spring-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: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.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">


    <!--配置自定義掃描包-->
    <context:component-scan base-package="hao.web"></context:component-scan>
    <!--對映物理路徑-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--字首-->
        <property name="prefix" value="/WEB-INF/pages/"/>
        <!--字尾-->
        <property name="suffix" value=".jsp"/>
        <!--比如在IndexController中return了“index”,這樣根據字首和字尾最終匹配到/WEB-INF/pages/index.jsp檔案-->
    </bean>

</beans>

IndexController

package hao.web;

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

@Controller
public class IndexController {

    @RequestMapping("/index")
    private String index(){
        return "index";
    }
}

依賴jar包

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.9.RELEASE</version>
    </dependency>

在這裡插入圖片描述
注意該檔案一定要放在WEB-INF目錄下,而且名字不能改變