閱讀664 返回首頁    go 阿裏雲 go 技術社區[雲棲]


spring mvc配置

第一
引入jar包
image

第二
web.xml 文件修改

<servlet>
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 配置Spring MVC 下的配置文件的位置和名稱 -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

第三
寫controller

@Controller
@RequestMapping("/lp")
public class HelloWorld {

    @RequestMapping("/he")
    @ResponseBody
    public Map<String,String> show(){
        Map<String,String> map = new HashMap<String,String>();
        map.put("1", "2");
        map.put("2", "2");
        map.put("3", "2");
        map.put("4", "2");
        map.put("5", "2");
        map.put("6", "2");
        return map;
    }
}

第四
編寫springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
    xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="https://www.springframework.org/schema/context"
    xmlns:mvc="https://www.springframework.org/schema/mvc"
    xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-4.0.xsd
        https://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

        <mvc:annotation-driven />
        <!-- 注意:當springMVC-servlet.xml中使用<mvc:annotation-driven />時,如果是3.1之前已經默認注入AnnotationMethodHandlerAdapter,3.1之後默認注入RequestMappingHandlerAdapter隻需加上上麵提及的jar包即可! -->
        <!-- 配置自動掃描的包 -->
        <context:component-scan base-package="com.lium"></context:component-scan>

        <!-- 配置視圖解析器 如何把handler 方法返回值解析為實際的物理視圖 -->
        <bean >
            <property name = "prefix" value="/WEB-INF/views/"></property>
            <property name = "suffix" value = ".jsp"></property>
        </bean>
</beans>

另:目錄結構
image

最後更新:2017-06-14 11:32:08

  上一篇:go  網站製作建設的基礎知識
  下一篇:go  阿裏推出業界首個非侵入式熱修複方案Sophix,顛覆移動端傳統發版更新流程!