1.1 简单示例
- 通篇使用java config
- @Controller声明bean是一个控制器
- @RequestMapping访问路径和方法之间的映射
1.2 演示
1.2.1 新建maven web项目
1.2.2 添加spring mvc依赖到maven
将pom.xml
修改如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>testSpringMVC</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<!-- Generic properties -->
<java.version>1.7</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Web -->
<jsp.version>2.2</jsp.version>
<jstl.version>1.2</jstl.version>
<servlet.version>2.5</servlet.version>
<!-- Spring -->
<spring-framework.version>4.1.5.RELEASE</spring-framework.version>
<!-- Logging -->
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- Other Web dependencies -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp.version}</version>
<scope>provided</scope>
</dependency>
<!-- Spring and Transactions -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>${logback.version}</version>
</dependency>
</dependencies>
<groupId>com.wisely</groupId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
1.2.3 构建目录
- 删除
src/main/webapp/WEB-INF/web.xml
; - 删除
src/main/resources
下的除logback.xml
; - 在
logback.xml
添加<logger name="org.springframework.web" level="DEBUG"/>
,观察请求错误(4xx错误); - 在
src/main/java
新建packagecom.wisely
; - 目录结构如图
1.2.4 基于java config的spring mvc的配置
本例基于java config,包括不使用web.xml
- Spring MVC的配置
package com.wisely;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
@Configuration
@ComponentScan("com.wisely")
@EnableWebMvc
public class DemoMVCConfig extends WebMvcConfigurerAdapter {
//名称与实际的页面的映射
// return "index" ; 实际返回的页面是/WEB-INF/views/index.jsp
@Bean
public UrlBasedViewResolver viewResolver(){
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
}
- WebInitializer(相当于web.xml)
package com.wisely;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import javax.servlet.jsp.jstl.core.Config;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class WebInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext)
throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(DemoMVCConfig.class);
//注册spring mvc的DispatcherServlet
ctx.setServletContext(servletContext);
Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}
1.2.5 简单的Controller
- HomeController
package com.wisely.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping("/")
public String home(){
return "index";
}
}
src/main/webapp/WEB-INF/views/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>Insert title here</title>
</head>
<body>
index.jsp is here
</body>
</html>
1.2.6 运行
新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。
京东地址:http://item.jd.com/11894632.html
当当地址:http://product.dangdang.com/23926195.html
亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6
淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail
或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。
相关推荐
细胞分化是生物体发育过程中的关键环节,它使得单一的受精卵细胞逐渐发展成为具有各种不同特性的细胞群体,最终构建出复杂的生命体。在医学领域,理解细胞分化对于研究胚胎发育、组织修复和疾病的发生机制至关重要。...
国元点睛-20230825-国元期货-23页
**STASM4.1版本-人脸识别77个特征点提取** STASM,全称为Shape Tracking and Analysis by Shape Models,是一种广泛应用于人脸识别领域的算法,尤其在面部特征点定位方面表现出色。该算法的核心在于通过形状模型来...
bugscan发展历程以及框架改进.pdf 基于正则类sql注入防御的绕过技巧.pdf 如何优雅的穿透linux内网.pdf 花式玩转bugscan插件.pdf 过滤型插件与反向代理的火花.pdf
alien skin Eye Candy 7是一款非常强大的PS滤镜插件。包含了金属字、火焰、闪电、纹理等30多种实用特效,是设计师必备的插件之一。
使用OpenCV1.0实现了眼睛状态识别,首先用harr检测眼睛,然后用模板匹配的方法标记眼睛状态。 程序需要使用Eyes45x11.xml Mouth25x15.xml Nose25x15.xml,将其放到C:\Program Files\OpenCV\data\haarcascades目录下...
眼睛穴位图-睛明穴-取穴方法 方便经常上网的朋友使用
把网页背景变成眼睛保护色或适合夜晚阅读的夜间模式,一些无法变色的小区块可以通过单击进行变色;
软件名:点睛文本编码查询 功能:本软件用于将文本的字符串转换为 Text、ASCII、Default、Unicode、BigEndianUnicode、UTF-8、UTF-7、GBK、BIG5 之间的互相转换,用于文本查询。转换结果使用十六进制表示。
点睛网赚论坛源码 网赚项目平台搭建源码+10W数据【送百度秒收录神器】 安装教程https://www.ygwzjs.cn/post/260.html 效果展示http://z-www.seoheimao.cn/ 点睛网论坛整站源码程序+10W+数据(送主动推送工具) ...
点睛网赚论坛源码整站程序+10W数据【送主动推送工具】 安装教程https://www.ygwzjs.cn/post/260.html 百度推送工具https://www.ygwzjs.cn/post/181.html 效果演示:z-www.seoheimao.cn 【全新优化】点睛网论坛整站...
《360点睛营销平台多语言加密及调试技术详解》 360点睛营销开放平台是一款由360公司推出的广告投放和营销管理工具,它为开发者提供了丰富的API接口,以帮助开发者构建和优化自己的广告系统。在这个名为"360点睛多...
中文说明 | |说明维护过项目的人应该都有体会, 如果接口文档是单独编写的(org-mode、markdown、rap 甚至是 word 等), 随着项目周期的推进, 接口文档和真实代码之间的差距会越来越远.基于 swagger 的一些细节不那么让...
通过这个Demo,开发者可以快速掌握虹软人脸识别V4.1在C#环境下的应用方式,为开发自己的人脸识别应用打下坚实的基础。同时,理解并运用相关算法和库,可以提升应用的智能化水平,满足不同场景的需求。
"点睛内码转换器"是一款专为处理文本编码问题设计的工具,它能够帮助用户在不同的字符编码之间进行转换,以解决由于编码不匹配导致的乱码问题。在计算机科学领域,字符编码是用于表示文本的一系列规则,常见的有...
360点睛使用手册是360官方于2019年推出的移动客户端软件,主要功能是推广管理。该手册详细介绍了软件的使用方法和功能特点,适用于广告主进行随时随地的账户投放管理。 首先,手册中的产品介绍部分指出,360点睛是...
【标题】"点睛网赚论坛整站程序+全套数据.zip"所涵盖的知识点主要集中在网站建设和运营领域,特别是关于论坛程序和数据管理方面。这个压缩包文件提供了一个完整的网络赚钱论坛的源代码和所有相关数据,对于想要搭建...
点睛字符替换器v1.05特别修正版是一款针对文本处理的专业软件,主要用于批量替换文本中的特定字符或字符串。这个特别修正版可能是对原始版本的优化和改进,以解决某些问题或添加新功能。在IT领域,字符替换工具是...
点睛字串替换器
点睛字串替换器V1·06主要用于非资源 本软件是一个本地化工具,主要用于非资源格式的本地化工作,支持的种类包括非资源格式的 C 编译的程序中的 ASCII 字符串和 UniCode 字符串、非资源格式的 Delphi(C++ Builder)...