首页 技术 正文
技术 2022年11月14日
0 收藏 447 点赞 4,787 浏览 3326 个字

完整的项目案例: springmvc.zip

介绍

SpringMVC是一款Web MVC框架。 它跟Struts框架类似,是目前主流的Web MVC框架之一。

文章通过实例来介绍SpringMVC的入门知识。

目录

Spring MVC HelloWorld入门及运行机制 (一)

实例

maven依赖springmvc jar:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>

项目结构路径:

Spring MVC HelloWorld入门及运行机制 (一)

一、配置web.XMl:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"> <!-- 配置请求总控器,由Spring-webmvc提供,它负责接收所有客户端的请求
,然后通过解析url并交给后端控制器(Controller)进行请求处理-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- springmvc默认会从WEB-INF下查找名为
[servlet-name]-servlet.xml的配置文件,
也可以通过init-param重新指定配置文件的路径-->
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 指定从classpath中查找 -->
<param-value>classpath:dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping></web-app>

二、配置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"> <!-- 启用注解并扫描 -->
<context:component-scan base-package="edu.nf.ch01.controller"/>
<!-- 启用mvc注解驱动-->
<!-- 这个注解驱动注册了RequestMappingHandlerMapping(请求映射处理器)
和一个RequestMappingHandlerAdapter(请求映射适配器),同时启用了
@RequestBody,@ResponseBody注解以及表单验证的等功能-->
<mvc:annotation-driven/> <!-- 配置视图解析器,spring有很多种视图解析器,不同的解析器用于解析成不同的视图对象
其中有一个叫做InternalResourceViewResolver(内部资源解析器)
,用于解析内部的jsp资源-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 给这个视图解析器注入两个属性 -->
<!-- 指定资源目录的前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 指定资源的后缀-->
<property name="suffix" value=".jsp"/>
<!-- 这个解析器默认解析的视图对象为InternalResourceView,
如果需要在JSP中使用jstl标签库,那么可以设置为JstlView,
它是InternalResourceView的子类-->
<!--<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>-->
</bean>
</beans>

三、控制器Controller类:

package edu.nf.ch01.controller;import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;/**
* @author wangl
* @date 2018/10/26
* 后端控制器
*/
@Controller
@Scope("prototype")
public class HelloController { /**
* 通过@RequestMapping来映射请求的url
* @return
*/
@RequestMapping("/hello")
public ModelAndView hello(){
System.out.println("hello spring");
ModelAndView mv = new ModelAndView("index");
return mv;
}
}

四、请求转发的页面

<%--
Created by IntelliJ IDEA.
User: wangl
Date: 2018/10/26
Time: 10:50
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>Welcome!</h1>
</body>
</html>

Spring MVC HelloWorld入门及运行机制 (一)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295