spring入门(五)【springMVC环境搭建】

springMVC作为spring的一个WEB组件,是一个MVC的思想,减少了WEB开发的难度,现介绍springMVC环境的搭建,具体的原理放在后面介绍。用过框架的朋友都知道要在WEB项目中使用一个框架,必须要引入这个框架,和其他框架的引用方式一样,springMVC的引入方式是通过DispatcherServlet,那么我们就要在web.xml中配置此servlet,在lib目录下我已经把用到的jar包全部导入,下面看我的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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>springmvc</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <!--log4j的配置文件-->
      <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/classes/log4j.properties</param-value>

    </context-param>
    <!--spring的配置文件-->
   <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application-context.xml</param-value>
    </context-param>

  <!--配置springmvc-->
  <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--springmvc的配置文件-->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/springmvc.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>

<!--配置spring-->
  <listener>
      <listener-class>
      org.springframework.web.context.ContextLoaderListener
      </listener-class>
  </listener>
  <!--配置系统中的日志-->
    <listener>
        <listener-class>
            org.springframework.web.util.Log4jConfigListener
        </listener-class>
    </listener>
</web-app>

springMVC

在配置文件中我们配置了DispatherServlet,并且配置了它的初始化参数contextConfigLocation,指定了文件所在的路径为:/WEB-INF/classes下;配置了拦截的URL,我们这里拦截所有的url

spring

你可能会疑惑,这里我们为什么又配置了spring,难道只有springmvc不可用吗?答案是可以的,我们完全可以把所有的配置都放在springmvc的配置文件中,但是我们这里遵循层次化的配置,让springMVC是一个配置文件,spring是一个配置文件,把他们所要完成的功能分开,其实所有的配置完全可以由dispatherServlet进行加载。

spring的加载方式这里使用了一个监听器,ContextLoaderListener,他会读取context中的配置参数:contextConfigLocation,读取spring的配置文件,加载spring容器。

log4j

由于在项目中要使用日志功能,所以这里配置了日志,且引用context中的配置文件。

下面看springmvc的配置文件,

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
    >
    <!--配置了组件扫描,就不需要配置<context:annotation-config> ,因为组件扫描已经包含了,
    组件扫描,扫描的注解有@Controller,@Service,@Resposity,@Component,
    @Controller注解必须由dispatherDispatcherServlet来扫描,
    也实现了依赖注入,对
    @Autowired,@Resource的支持
    -->
    <context:component-scan base-package="com.cn.my">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--开启基于注解的驱动-->
    <context:annotation-config></context:annotation-config>
    <!--开启mvc的注解驱动 ,可以使用@RequestParam注解,可以将请求参数帮到到控制器参数上-->
    <mvc:annotation-driven/>

    <mvc:resources location="/resource/*" mapping="/resource/**"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>

    </bean>
</beans>

上面是springmvc的配置文件,首先配置了组件扫描,组件扫描的作用是会自动类级别的注解(@Controller、@Component、@Service、@Resposity),这里配置了只扫描带有@Controller注解的类,是因为具有@Controller注解的类只能由DispatherServlet加载,接着开启了基于注解的驱动,此驱动可以实现依赖注入,对@Autowired、@Resource注解起作用,其实可以不配置,因为在组件扫描中已经包含了此功能;基于mvc的注解驱动,可以方便将请求参数邦定到控制器参数上。
最下面配置了视图解析器,这里使用的默认的视图解析器。

再看spring的配置文件,

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
    >

	<context:component-scan base-package="com.cn.my">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>

	<!--配置一个数据源,使用spring提供的数据源-->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"></property>
		<property name="username" value="root"></property>
		<property name="password" value="123456"></property>
	</bean>
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>

	</bean>
</beans>

  spring的作用可以实现数据源的配置,事务或者service层的依赖注入,首先是组件扫描,扫描的对象是除了@Controller的注解,接着是一个数据源,这里使用了spring提供的数据源,其他还可以使用DBCP、C3P0、JNDI等方式,在后边讨论。

然后配置了一个数据访问对象JdbcTemplate,通过此对象可以对数据库进行操作。

至此我们的环境已经搭建完毕,下面是具体的使用,

编写Controller类,

由于我们使用了组件扫描,所以在类上使用注解@Controller

package com.cn.my.controllor;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.cn.my.service.CourseServiceInter;

@Controller
@RequestMapping("/my/*")
public class MyController {

    @Autowired
    private CourseServiceInter csi;

    //一般类型的url

    @RequestMapping("my")
    public String method(HttpServletRequest request,@RequestParam("courseId") String courseid){

        csi.findCourse(courseid);
        return "success";
    }
    //restful形式的URL
    //http://localhost:8080/my/my2/12
    @RequestMapping("my2/{courseId}/{name}")
    public String method2(@PathVariable("courseId") String courseId,@PathVariable("name") String name){
        csi.findCourse(courseId+"  "+name);
        return "success";
    }
}

除了在类上使用了@Controller注解,还是用了@RequestMapping注解,注明访问的路径,接着在方法method上又使用@RequestMapping注解,一个完整的url访问路径由类上的路径加方法上的路径组成,例:/my/my.do,这就是一个访问路径,我们还使用了service层的一个findCourse方法,service层的对象有框架依赖注入;在方法中使用了@RequestParam注解,此注解可以把url中的参数邦定到方法的参数上,其中@RequestParam中的值要和url中的请求参数名一致,后面的则是对应的方法中参数名。方法最后返回的是一个String类型的值,这里返回的是逻辑视图名,也可以返回JSON串,这种方式在后边介绍。

controller配置好之后,便可以访问了,这样一个sprigMVC的环境及controller的编写就完成了。

有不正之处欢迎指出,谢谢!

时间: 2024-07-30 13:48:23

spring入门(五)【springMVC环境搭建】的相关文章

SpringMVC环境搭建(xml版本)

SpringMVC是Web层框架,它隶属于Spring框架,只是Spring这个庞大框架下的一个小模块,用于替换Servlet 1.下载jar包(20) http://zhidao.baidu.com/link?url=guH_VTC2FKGftWBtdCW_AU_z9t5QgyGqlr-DEwkryPCNhhlySr9wJACJVJu3hRiib50j_GNFkQNxZr4-k2v3z5jRpS1poxR3YZeb5dfThuS我是根据这个网址找到一个压缩包,解压后libs下会发现很多jar包

Mule 入门之:环境搭建

Mule 入门之:环境搭建 JDK1.5或以上版本Eclipse3.3以上 下载与安装:目前最新版本为2.2.1 下载,下载后得到一名为mule-standalone-2.2.1.zip的压缩文件,解压到本地,如:H:\mule-2.2.1,再在环境变量中设置变量:MULE_HOME,值为H:\mule-2.2.1, ====================================================================== ===================

Git入门篇之环境搭建&基本功能的使用

网上关于GitHub的使用教程还是比较丰富,不过部分教程比较陈旧抑或写得不够详细,在我实践的过程中遇到了一些麻烦,记录下来,当是自己的一个总结吧,也供大家参考.欢迎留言交流. 本文主要讲解Windows Msysgit软件平台的使用,windows的GitHub for Windows方式和苹果系统的Github或git方式也在用,这方面网上的教程比较丰富,也就不做过多阐述了,欢迎留言讨论. Windows版本: 在windows中搭建Git环境使用Github有两种方式: 方式一: GitHu

[精华]springMVC环境搭建基础入门总汇!

1.SpringMVC概述 SpringMVC 是一种基于 Java 的实现 MVC 设计模型的请求驱动类型的轻量级 Web 框架,属于 Spring       FrameWork 的后续产品,已经融合在 Spring Web Flow 里面.Spring 框架提供了构建 Web 应用程序的全功 能 MVC 模块.使用 Spring 可插入的 MVC 架构,从而在使用 Spring 进行 WEB 开发时,可以选择使用 Spring 的 Spring MVC 框架或集成其他 MVC 开发框架,如

SpringMVC环境搭建

springmvc和stuts2相同是实现mvc的优秀框架,进行业务逻辑控制.最大的差别就是struts2通过filter拦截请求到Action.而spring用的是servlet获取请求到controller.再一个是springmvc不用过多进行配置,能够通过注解非常方便简单的进行开发. SpringMVC+Spring+Hibernate环境搭建: 1 导入相应jar包: 2 在web.xml配置Spring的监听器,指定配置.配置SpringMVC的核心Servlet,指定哪些请求由Sp

springmvc环境搭建以及常见问题解决

1.新建maven工程 a)  打开eclipse,file->new->project->Maven->Maven Project b)  下一步 c)   选择创建的工程为webapp,下一步 d)   填写项目的group id和artifact id.一般情况下,group id写域名的倒序,artifact id写项目名称即可.最后点完成. e)   最初建好后,项目目录结构如下 f)   一般的项目目录中,还有src/main/java,src/main/test/ja

聊聊Spring boot2.X开发环境搭建和基本开发

对Spring Boot的开发环境进行搭建,并对它的特点做进一步的了解,才能更好地对Spring Boot有更深入的介绍.但是无论如何都需要先来搭建Spring Boot的工程. 搭建Spring Boot开发环境 使用Spring Boot,首先需要搭建一个快速开发的工程环境.Spring Boot工程的创建存在多种方式,但是因为当前Eclipse和IntelliJ IDEA这两种IDE的广泛应用,所以本书只介绍这两种IDE环境下的搭建. 搭建Eclipse开发环境 首先找到Eclipse的菜

SpringMVC环境搭建之Idea

SpringMVC一.. SpringMVC重要组件介绍    1. DispacherServlet:前端控制器,接收所有请求,(如果配置/不包含jsp)    2. HandlerMapping:解析请求格式,判断希望要执行哪个方法    3. HandlerAdapter:负责调用具体的方法    4. ViewResovler:视图解析器,解析结果,准备跳转的具体的视图 spring的jar包官方下载地址: https://repo.spring.io/libs-release-loca

力所能及之springmvc环境搭建

小狼从12年学习java开始,就听老师说了若干springmvc的高大上,可是人家就吊你胃口,死活不给你讲.当然,小狼不是盈利机构,不需要赶时间,这个该讲,那个不该讲的.今天,小狼就说说springmvc的环境搭建. 开发环境:myeclipse8.6  spring4.0 开发需要的jar文件: 项目结构: web.xml配置: <?xml version="1.0" encoding="UTF-8"?> <web-app version=&qu