美文网首页
Spring MVC 入门,搭建SpringMVC框架。

Spring MVC 入门,搭建SpringMVC框架。

作者: 我是隔壁老陈 | 来源:发表于2018-12-04 14:54 被阅读0次
宝儿姐镇楼

写这篇文章的目的:记录学习,方便查阅,同时能为看到这篇文的朋友提供一些思路或者少走些弯路就更开心了。

本文所介绍方式为手动引入jar包(非Maven)

不多废话,直接开始。

环境

  • eclipse oxygen
  • mysql5.7(暂时未用到。以后可能用)
  • jre1.8.0
  • tomcate 7.0

步骤

  1. 新建项目
    新建一个donamic web project。完成后的目录像下图这样

在这一步中,经常有人可能会发现新建后的项目WEB-INF文件夹下同时创建了web.xml文件,而其他人可能没有(我这里就没有)。
解决方法很简单:右键点击项目名——Java EE Tools——Generate Deployment Descriptor Stub。然后神奇的事情就会发生了。


注:这个文件也可以自己手动添加。

  1. 新建配置文件
    SpringMVC特点之一就是Ioc,即依赖注入。依赖注入所需要的就是配置文件,所注入的bean都在配置文件中进行配置。
  • 修改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" version="3.0">
  <display-name>Article</display-name>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext.xml的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。 -->
   <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
    <!-- 读取applicationContext.xml配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
  
  <!-- 配置DispatchcerServlet(spring view分发器) -->
    <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>/WEB-INF/springmvc.xml</param-value>
        </init-param>
        <!-- 启动时就加载Servlet -->
        <load-on-startup>1</load-on-startup>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
  • <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    这行配置的是applicationContext.xml文件位置,如果不进行配置,默认为WEB-INF下
  • 配置的路径问题:配置为“/WEB-INF/springmvc.xml”时,将配置文件放在WEB-INF下。而“classpath:applicationContext.xml”则将配置文件放于src下。
    原因:classpath 路径在每个J2ee项目中都会用到,即WEB-INF下面的classes目录,所有src目录下面的java、xml、properties等文件编译后都会在此,所以在开发时常将相应的xml配置文件放于src或其子目录下;
  • 新建springmvc.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.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
 
 
        <!-- 配置自动扫描的包 -->
        <context:component-scan base-package="com.ftwj.*"></context:component-scan>
        
        <!-- 开启注解功能 -->
        <mvc:annotation-driven />
    
        <!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
 
 
        <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name = "prefix" value="/WEB-INF/views/"></property>
            <property name = "suffix" value = ".jsp"></property>
        </bean>
        
        <!-- 静态文件(images,css,js)的访问路劲的配置,即resource下的文件夹,包括子文件夹 -->
        <mvc:resources  mapping="/resources/**" location="/resources/"/>
        <!--   <mvc:resources  mapping="/uploadFile/**" location="/uploadFile/"/>
        
        <mvc:resources  mapping="/products/**" location="/products/"/>
        <mvc:resources  mapping="/ueditor/**" location="/ueditor/"/> -->
        
       
</beans>
  • applicationContext.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:p="http://www.springframework.org/schema/p" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:task="http://www.springframework.org/schema/task"
    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.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task.xsd">
    
    <!-- 加载jdbc.properties文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    
    <!-- 数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${oracle.driver}" />
        <property name="url" value="${oracle.url}" />
        <property name="username" value="${oracle.username}" />
        <property name="password" value="${oracle.password}" />
    </bean>
      
    <!-- 创建jdbcTemplate模板 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
        p:dataSource-ref="dataSource" />
        
    <!-- 创建namedParameterJdbcTemplate模板 -->
    <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
        <constructor-arg ref="dataSource"/>
    </bean>
        
    <!-- 创建事务管理器 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
</beans>

对应的数据库配置文件jdbc.properties如下:

oracle.driver=com.mysql.jdbc.Driver
oracle.url=jdbc:mysql://(ip):(端口号)/(数据库名)?useUnicode=true&characterEncoding=UTF-8
oracle.username=(登录名)
oracle.password=(密码)

注意:这里都填写自己的信息(ip,端口号,登陆名,密码等),记得删掉括号~

一切都完成后的项目目录如下

  1. 导入jar包
    需要导入springmvc相关包,druid数据源jar包,有数据库连接还需导入数据库连接相关包,使用了log4j则需导入log4j相关包,还有使用了json的话需导入json相关包......
    此处导入springmvc、druid和数据库连接包如下:

  2. 写一个测试页面
    在webcontent目录下新建一个index.jsp页面。

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <p>这里是index~</p>
</body>
</html>
  1. OK,一切就绪,接下来要做的事就是将项目发布到tomcate中,启动tomcate,打开浏览器输入地址进行测试了。
    tomcate和eclipse怎么弄请自行百度(我也忘记了)。。。
    tomcate启动成功:

    打开浏览器,输入"http://localhost:8080/SpringMVC/",结果如下说明正确:

后续会根据这个架子继续做一些扩展,转载请注明出处。
在下才疏学浅,如有错误欢迎各位大佬指出并批评指正~

相关文章

网友评论

      本文标题:Spring MVC 入门,搭建SpringMVC框架。

      本文链接:https://www.haomeiwen.com/subject/tfhucqtx.html