搭建一个SpringMVC项目

程序实现:

1、新建项目:File-New,选择Dynamic web project,并取项目名称为MVCProject
在这里插入图片描述
2、添加配置文件:在WEB-INF文件夹下面添加web.xml和springmvc.xml文件:
web.xml文件:
在这里插入图片描述
注意:
A、init-param里的内容用于配置spring mvc的配置文件的位置和名称,这里说明会新建一个springmvc.xml的配置文件
B、 我们也可以不新建springmvc.xml,而是用默认的,默认的配置文件格式为/WEB-INF/[servlet-name]-servlet.xml,对应这里的就是springDispatcherServlet-servlet.xml
C、这里的servlet-mapping表示拦截的模式,这里是“/”,表示对于所有的请求的拦截,包括静态资源如html, js, jpg等。这时候对于静态资源的访问就会报404的错误。关于如何解决后面会介绍

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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
        <!-- 配置自动扫描的包 -->
        <context:component-scan  base-package="com.zx.controller"></context:component-scan>
        <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
        <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name = "prefix" value="/WEB-INF/views/"></property>
            <property name = "suffix" value = ".jsp"></property>
        </bean>
</beans>

注意:
A、base-package表示spring监听的范围,这里是在com.zx.controller下
B、class=“org.springframework.web.servlet.view.InternalResourceViewResolver”,是添加了一个视图解析器,用于把在控制器中 handler的结构解析为实际的物理视图,这个要配合controller类来解析。

3、添加Spring-framework框架相关jar包:
Spring-framework 5.1.0 下载地址:https://pan.baidu.com/s/1jsGJVmB5iQ_8gQ437mmZuw

在这里插入图片描述
4、在WebContent文件夹下面添加index.jsp文件:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>界面标题</title>
</head>
<body>
<a href="upload/helloworld">hello world</a>
</body>
</html>

当访问index.jsp时,页面上会展示一个超链接,点击超链后,url中的地址就会发生跳转,由“http://localhost:8080/MVCProject/index.jsp”跳转到“http://localhost:8080/MVCProject/upload/helloworld”,而这个url请求就会进入FileUploadController中的hello方法,因为其与该方法上的“/helloworld”匹配。
在这里插入图片描述
4、在WEB-INF/views/下面添加success.jsp文件:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>成功页面</title>
</head>
<body>
<h4>恭喜您成功了</h4>
</body>
</html>

5、建立FileUploadController类(包名com.zx.controller下):

package com.zx.controller;

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

@Controller
@RequestMapping("/upload")
public class FileUploadController {
    
    @RequestMapping("/helloworld")
    public ModelAndView hello(){
        ModelAndView view = new ModelAndView("success");
        return view;
}
    
}

注意:
A、首先要在类的前面添加“Controller”注解,表示是spring的控制器,这里会写一个方法hello()
B、hello方法上方有一个@RequestMapping, 是用于匹配请求的路径,比如这里匹配的请求路径就是“http://localhost:8080/MVCProject/upload/helloworld”,即当tomcat服务启动后,在浏览器输入这个url时,如果在这个方法打断点了,就会跳入该方法。
C、这个return的结果不是乱写的,这个返回的字符串就是与上面springmvc.xml中line15-18进行配合的,springmvc.xml中声明了prefix和suffix,而夹在这两者之间的就是这里返回的字符串,所以执行完这个方法后,我们可以得到这样的请求资源路径“/WEB-INF/views/success.jsp”,这个success.jsp是需要我们创建的。

三、值得注意的问题
1、tomcat服务器的添加
从前面的介绍可以看出,我们的程序是通过浏览器发请求来获取想要的页面,那么这里就免不了要有一个web服务器,这里就是tomcat。
首先你需要下载个tomcat,然后在eclipse->windows->preference->servers中绑定这个tomcat服务器;
其次你需要在你新建的spring mvc项目中添加tomcat的支持,否则在新建的jsp文件中会提示报错“The superclass javax.servlet.http.HttpServlet was not found on the Java Build Path”
右键项目->build path->configure build path->add library->server runtime, 选择你的tomcat即可
在这里插入图片描述
2、spring mvc如何访问静态资源
采用spring自带mvc:resources方法。首先找到你定义的那个servlet的xml文件,如本例子中,servlet的名字叫springmvc.xml,因此需要找到springmvc.xml文件,并在该文件中插入以下配置:

<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**/" location="/resources/"/>

如此就不必另外添加一个mvc来处理静态资源。而mvc知道静态资源所处的位置为resources文件夹。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值