Spring MVc入门之环境搭建(一)

虽然大多数时间使用Spring MVC开发,但是使用现有的框架结构开发居多,到现在又快有点不会搭建spring mvc项目,巩固一下,就再搭建一次,记录一下当做资料。

开发环境:eclipse、JDK1.7  tomcat7.0

1.Spring MVC所需要的jar包:

commons-logging-1.2.jar、
spring-aop-4.2.3.RELEASE.jar、
spring-beans-4.2.3.RELEASE.jar、
spring-context-4.2.3.RELEASE.jar、
spring-core-4.2.3.RELEASE.jar、
spring-expression-4.2.3.RELEASE.jar、
spring-web-4.2.3.RELEASE.jar、
spring-webmvc-4.2.3.RELEASE.jarcommons-logging-1.2.jar、
spring-aop-4.2.3.RELEASE.jar、
spring-beans-4.2.3.RELEASE.jar、
spring-context-4.2.3.RELEASE.jar、
spring-core-4.2.3.RELEASE.jar、
spring-expression-4.2.3.RELEASE.jar、
spring-web-4.2.3.RELEASE.jar、
spring-webmvc-4.2.3.RELEASE.jar

2.工程项目结构图:

3.配置文件 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app  version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee
                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                                            http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
     <context-param>
        <param-name>contextConfigLocation</param-name>
        <!-- 默认是/WEB-INF/applicationContext.xml -->
        <param-value>/WEB-INF/applicationContext.xml</param-value>
     </context-param>
     <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
     </listener>
    <!-- spring MVC的入口 -->
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/SpringMVC-servlet.xml</param-value>
            <!-- 默认是/WEB-INF/[servlet名字]-servlet.xml -->
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

applicationContext.xml

就一个空内容的xml文件

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-4.0.xsd">

</beans>

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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
    <!--扫描路径(设置使用注解的类所在的jar包)-->
    <context:component-scan base-package="com.springmvc.controller" />
    <!-- 激活基于注解的配置 @RequestMapping, @ExceptionHandler,数据绑定 ,@NumberFormat ,
    @DateTimeFormat ,@Controller ,@Valid ,@RequestBody ,@ResponseBody等  -->
    <mvc:annotation-driven />
    <!-- 静态资源处理, css, js, imgs -->
<!--     <mvc:resources location="/assets/" mapping="/assets/**"></mvc:resources> -->
    <!-- 视图层配置 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

4.Controller类

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

/** 
 * 类说明:
 * 创建时间:2017年11月3日 下午12:11:01
 * @author 刘俊
 * @version 1.0
 * @since JDK 1.7
 */
@Controller
public class MvcController {
 @RequestMapping("/hello")
    public String hello(Model model){    
        model.addAttribute("test", "Controller return value :test");
        return "hello";
    }

}

5.页面hello.jsp(和Controller里面方法返回的名称一致)

<html>
<body>
<h1>this is my spring mvc</h1> 
<h2>Hello World! i am hello.jsp</h2>
<br>
<h1>${test}</h1>
</body>
</html>

以上就是项目结构及代码片段;

源码下载:https://gitee.com/gulang1/springMvc-helloworld.git





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值