SpringMVC 基础笔记

2016-10-31  本文已影响13人  爆炸的榴莲

一、下载

先去Spring官网看下springframework的最新版本号,或者是就用你想要下的版本,替换下面url中的版本号,就可以了
http://repo.springsource.org/libs-release-local/org/springframework/spring/4.2.1.RELEASE/spring-framework-4.2.1.RELEASE-dist.zip

再下一个依赖包apache commons logging
百度上搜一下,应该就有下载的

二、配置

我用的开发工具是eclipse for javaEE

打开新建一个web项目, Dynamic web module version 选择2.5或者3.0以上double没关系,选择3.0以上的不要直接finish,一步一步的next,直到看到Generate web.xml deployment descripter选项,请打上勾,finish

在web.xml中配置

  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 这个是设置spring配置文件路径的,可以不配置-->
<!--    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:S.xml</param-value>
    </init-param> -->
    
    <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

在WEB-INF下新建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:context="http://www.springframework.org/schema/context"     
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        
    </beans>

三、spring HelloWorld项目###

在springMVC-servlet.xml文件beans标签内加上

        <!-- 视图分解器 -->
        <bean id="viewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- 前缀 -->
            <property name="prefix" value="/"></property>
            <!-- 后缀 -->
            <property name="suffix" value=".jsp"></property>
        </bean>
        <!-- 启用spring mvc 注解 -->
        <context:annotation-config />
    
        <!-- 设置使用注解的类所在的jar包 -->
        <context:component-scan base-package="controllers"></context:component-scan>
        <!-- 完成请求和注解POJO的映射 -->
        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

spring控制器返回的是一个字符串,最后产生的url是前缀+字符串+后缀

在工程src目录下新建一个controllers包,再新建一个类

    package controllers;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    @Controller
    public class TestController {
        
        @RequestMapping("/welcome")
        public String welcome(){
            return "welcome";
        }
    }

新建一个welcome.jsp文件

然后启动tomcat,把工程发布到tomcat,打开浏览器,输入http://localhost:8080/(项目名称)/welcome 回车

上一篇 下一篇

猜你喜欢

热点阅读