加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 01:14:19 所属栏目:大数据 来源:网络整理
导读:springMVC作为spring的一个WEB组件,是一个MVC的思想,减少了WEB开发的难度,现介绍springMVC环境的搭建,具体的原理放在后面介绍。用过框架的朋友都知道要在WEB项目中使用一个框架,必须要引入这个框架,和其他框架的引用方式一样,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-
            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;

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

 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);
        ;
    }
}

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

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

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

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读