Spring完整揭秘(十二):通过IDEA创建SpringMVC项目

45 篇文章 21 订阅

1、创建spring项目

  • 根据1,2,3,4顺序进行操作
    在这里插入图片描述

2、输入项目名称

在这里插入图片描述

3、确定maven配置

在这里插入图片描述

  • 在maven配置文件中设置国内阿里云镜像
  <!-- settings.xml -->
   ...
   <mirrors>
    <mirror>
        <id>alimaven</id>
        <mirrorOf>central</mirrorOf>
        <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
	</mirror>
  </mirrors>
  ...

4、确定项目路径

在这里插入图片描述

5、生成项目结构

在这里插入图片描述

6、添加Springmvc框架支持

  • 在项目目录上点击鼠标右键,并在弹出菜单中选择"Add Framework Support"
    在这里插入图片描述
  • 勾选Spring MVC ,并下载jar包
    在这里插入图片描述
    在这里插入图片描述
  • 添加springmvc框架后,在WEB-INF目录下生成了以下两个文件:
    在这里插入图片描述
  • applicationContext.xml及dispatcher-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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>

7、认识web.xml

  • SpringMVC框架的web应用,仍然是一个遵循servlet规范的web应用程序。
  • 但在SpringMVC框架的web.xml中,会出现一些新的元素。
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <!-- ContextLoaderListener的作用就是启动Web容器时,
    读取在contextConfigLocation中定义的xml文件,
    自动装配ApplicationContext的配置信息,并产生WebApplicationContext对象 
    比如:数据源(DataSource)定义,服务对象(Services)定义等-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!--DispatcherServlet是Spring MVC框架Web应用程序的Front Controller,
    负责几乎所有对应当前Web应用程序的Web请求的处理 -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.form</url-pattern>
    </servlet-mapping>
</web-app>

关于DispatcherServlet的工作原理,请浏览该文章:Spring完整揭秘(十一):通过三张图认识Spring MVC的架构

8、添加jetty服务测试

请浏览该文章:IntelliJ IDEA 配置Jetty启动SpringMvc项目

  • 注意:运行项目时出现"Could not instantiate listener org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException"错误时,把所有spring*.jar 都拷贝到 WEB-INF/lib根文件夹下

在这里插入图片描述

  • 运行项目
    在这里插入图片描述

9、重整项目结构,增加Controller层测试

  • 增加java文件夹(source目录)及demo.controller包结构,存放Controller控制器源码

  • 在WEB-INF目录下增加static目录,存放css,image,js等静态文件

  • 在WEB-INF目录下增加views目录,存放视图文件
    在这里插入图片描述

  • 修改dispatcher-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: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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--此文件负责整个mvc中的配置-->

    <!--启用spring的一些annotation -->
    <context:annotation-config/>

    <!-- 配置注解驱动 可以将request参数与绑定到controller参数上 -->
    <mvc:annotation-driven/>

    <!--静态资源映射-->
    <!--本项目把静态资源放在了webapp的statics目录下,资源映射如下-->
    <mvc:resources mapping="/css/**" location="/WEB-INF/statics/css/"/>
    <mvc:resources mapping="/js/**" location="/WEB-INF/statics/js/"/>
    <mvc:resources mapping="/image/**" location="/WEB-INF/statics/image/"/>

    <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀(如果最后一个还是表示文件夹,则最后的斜杠不要漏了) 使用JSP-->
    <!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用html)- -->
    <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/views/"/><!--设置JSP文件的目录位置-->
        <property name="suffix" value=".jsp"/>
        <property name="exposeContextBeansAsAttributes" value="true"/>
    </bean>

    <!-- 自动扫描装配 -->
    <context:component-scan base-package="demo.controller"/>
</beans>
  • 增加IndexController测试文件
@Controller
@RequestMapping
public class IndexController {

    @GetMapping("/index")
    public String index() {
        return "index";
    }
}
  • 修改web.xml
<web-app>
   ...
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
  • 重新运行项目
    在这里插入图片描述
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智慧zhuhuix

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值