springMVC的环境搭建

前言作为一位码农,总的给这世界留下点东西吧,左思右想,只能留下点代码了,因为除了代码几乎也没有什么东西可以留下,于是编写了第一篇博客。下面进入正题springmvc环境搭建。

其实springMVC环境搭建很简单主要有五步:

     一.新建项目 导入jar包

     二.配置web.xml

     三.在根目录下新建spring-mvc.xml

     四.新建一个Java测试类

       五.新建两个

一.第一步  新建项目 导入jar包,这些jar包都是必须的

  • spring-aop-4.0.4.RELEASE.jar
  • spring-beans-4.0.4.RELEASE.jar
  • spring-context-4.0.4.RELEASE.jar
  • spring-core-4.0.4.RELEASE.jar
  • spring-expression-4.0.4.RELEASE.jar
  • spring-web-4.0.4.RELEASE.jar
  • spring-webmvc-4.0.4.RELEASE.jar
  • commons-logging-1.1.1.jar(用来打印log)


二.第二步  配置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_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <!--工程名 -->
    <display-name>wea</display-name>

    <!-- 配置DispatchcerServlet -->
    <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:spring-mvc.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>
</web-app>


三.第三步   在根目录下新建spring-mvc.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/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">
    <!-- 声明自动扫描的包(仅含springMvc部分) -->
    <context:component-scan base-package="com.springmvc" use-default-filters="false">
        <!-- 包含@Controller注解 -->
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller"/>
        <!-- 包含@ControllerAdvice注解 -->
        <context:include-filter type="annotation"
            expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>

    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!--
    简写形式,自动配置
    使用了annotation-driven后,缺省使用DefaultAnnotationHandlerMapping 来注册handler method和request的mapping关系。
    未配置时,不能执行到根目录下的jsp。
    -->
    <mvc:annotation-driven></mvc:annotation-driven>
    
    <!-- 配置 MultipartResolver用于文件上传 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 默认字符编码 -->
        <property name="defaultEncoding" value="UTF-8"></property>
        
        <!-- 文件最大尺寸,单位是Byte,
            10485760=10M
            104857600=100M
            1073741824=1G
            当前4294967296=4G
        -->
        <property name="maxUploadSize" value="4294967296"></property>
        
        <!-- 上传文件的临时路径,文件上传完后,临时目录中的临时文件会被自动清除 -->
        <property name="uploadTempDir" value="upload_temp"></property>
    </bean>
</beans>

四.第四步  新建一个Java测试类
        package com.springmvc.test;

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

/**
 *
 * @author:admin
 * @time:2017年7月28日 下午4:59:25 类说明:测试springMVC是否配置好
 */
@Controller
public class test_springmvc {

    /**
     * 1. 使用RequestMapping注解来映射请求的URL 2. 返回值会通过视图解析器解析为实际的物理视图,
     * 对于InternalResourceViewResolver视图解析器,会做如下解析 通过prefix+returnVal+suffix
     * 这样的方式得到实际的物理视图,然后会转发操作 "/WEB-INF/views/success.jsp"
     *
     * @return
     */
    @RequestMapping("/helloworld")
    public String hello() {
        System.out.println("hello world");
        return "success";
    }
}

五、第五步  在 webcontent  新建 一个index.jsp 

        在jsp里  加上一个 a 标签   <a href="helloword">点击</a>

        在WEB-INF 下的views 中创建 一个 success.jsp 随便写点就行




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值