springboot实战第四章-SpringMVC项目快速搭建

springboot实战第四章-SpringMVC项目快速搭建

 Spring MVC 项目快速搭建

  Spring MVC使我们可以简单地开发灵活且松耦合的Web项目,本章将关注基于注解和Java配置的Spring MVC开发。

基本架构:Spring MVC 4.1.5+maven+logback+log4j

1.构建Maven项目

pom.xml内容:

<?xml version="1.0" encoding="UTF-8"?>
<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>

	<groupId>com.just</groupId>
	<artifactId>springmvc4learning</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<name>springmvc4Learning</name>
	<description>Demo project for Spring MVC4</description>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<!-- web -->
		<jsp.version>2.2.1</jsp.version>
		<jstl.version>1.2</jstl.version>
		<servlet.version>3.1.0</servlet.version>
		<!-- Spring -->
		<spring-framework.version>4.1.6.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>
			<!-- provided表示打包的时候不用包进去-->
			<scope>provided</scope>
		</dependency>

		<!-- Spring MVC -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring-framework.version}</version>
		</dependency>

		<!-- 其他web依赖-->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>${jstl.version}</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>${servlet.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>javax.servlet.jsp-api</artifactId>
			<version>${jsp.version}</version>
		</dependency>

		<!-- Spring and Transactions -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring-framework.version}</version>
		</dependency>

		<!-- 使用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>
		<!-- 对json和xml文件的支持-->
		<dependency>
			<groupId>com.fasterxml.jackson.dataformat</groupId>
			<artifactId>jackson-dataformat-xml</artifactId>
			<version>2.9.5</version>
		</dependency>
		<!-- file upload -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.1</version>
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.4</version>
		</dependency>
		<!-- test -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring-framework.version}</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.10</version>
            <scope>test</scope>
		</dependency>
	</dependencies>

	<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>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.3</version>
				<configuration>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
		</plugins>
	</build>


</project>

2.日志配置

在src/main/resources目录下,新建logback.xml来配置日志

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="1 seconds">
    <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
        <resetJUL>true</resetJUL>
    </contextListener>

    <jmxConfigurator/>
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>logback: %d{HH:mm:ss.SSS} %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    <logger name="org.springframework.web" level="DEBUG"/>
    <root level="INFO">
        <appender-ref ref="console"/>
    </root>
</configuration>

3.演示页面

在src/main/resources下新建views目录,并在此目录下新建index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>我是标题呀</title>
</head>
<body>
   <pre>
       Welcome to Spring MVC world!
   </pre>
</body>
</html>

注意:本人用的是idea开发的,在这个目录下右键新建jsp的时候发现没有jsp选项,这是因为默认的web的东西是在webapp目录下,所以要将工程web指向这个resource目录,具体可看截图:

springboot实战第四章-SpringMVC项目快速搭建

4.springmvc配置

package com.just.springmvc4.config;
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.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@Configuration
@EnableWebMvc
@ComponentScan("com.just.springmvc4")
public class MyMvcConfig {
@Bean
public InternalResourceViewResolver viewResolver(){
InternalResourceViewResolver resourceViewResolver=new InternalResourceViewResolver();
resourceViewResolver.setPrefix("/WEB-INF/classes/views/"); //运行时的目录
resourceViewResolver.setSuffix(".jsp");
resourceViewResolver.setViewClass(JstlView.class);
return resourceViewResolver;
}
}

5.web配置

package com.just.springmvc4.config;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
 * WebApplicationInitializer 是spring提供用来配置Servlet3.0+配置的接口,替代web.xml
 */
public class WebInitializer implements WebApplicationInitializer{
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext context=new AnnotationConfigWebApplicationContext();
        context.register(MyMvcConfig.class);
        context.setServletContext(servletContext);
        //注册DispatcherServlet
        ServletRegistration.Dynamic servlet=servletContext.addServlet("dispatcher",new DispatcherServlet(context));
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);

    }
}

WebApplicationInitializer是Spring提供用来配置Sevlet3.0+配置的接口,用来替换web.xml。

实现此接口会自动被SpringServletContainerInitializer获取到

6.简单控制器

package com.just.springmvc4.controller;

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

@Controller
public class HelloController {
    @RequestMapping("/index")
    public String hello(){
        return "index";
    }
}

7.部署,访问http://localhost:8080/index

springboot实战第四章-SpringMVC项目快速搭建

8.总结

springmvc架构零xml配置,简单粗暴的例子,后续会根据项目中常见的问题增加一些东西,书中没提到的还有很多。


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/100314.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 树莓派视觉小车 — 人脸追踪(人脸识别、PID控制舵机运动)[通俗易懂]

    树莓派视觉小车 — 人脸追踪(人脸识别、PID控制舵机运动)[通俗易懂]效果展示基础理论(人脸识别)人脸检测算法按照方法可以被分为两大类,基于特征的算法、基于图像的算法。1、基于特征的算法基于特征的算法就是通过提取图像中的特征和人脸特征进行匹配,如果匹配上了就说明是人脸,反之则不是。提取的特征是人为设计的特征,例如Haar,FHOG,特征提取完之后,再利用分类器去进行判断。通俗的说就是采用模板匹配,就是用人脸的模板图像与待检测的图像中的各个位置进行匹配,匹配的内容就是提取的特征,然后再利用分类器进行判断是否有人脸。…

    2022年5月9日
    38
  • linux 驱动移植_免驱动led灯好吗

    linux 驱动移植_免驱动led灯好吗通过前两篇文章的介绍,我们已经把linux内核移植到了tiny210上,但是看到的现象都是通过超级终端来观察的,下面了,我们介绍一下led灯的移植,给大家一个更直观的感受。这篇文章主要的内容如下:1.对平台总线的简介;2.led驱动的移植。一.平台总线   首先介绍一下,我们为什么要简单介绍一下平台总线呢?因为我们是做led驱动的移植,而不是自己编写led的驱动代码。我们要移植

    2022年9月25日
    0
  • linux 操作系统 哪个好用,一款非常好看好用的国产Linux操作系统发行版

    linux 操作系统 哪个好用,一款非常好看好用的国产Linux操作系统发行版原标题:一款非常好看好用的国产Linux操作系统发行版之前在网上看到有网友说,国产操作系统的界面不好看,很简陋很粗糙,就像是WindowsXP的那种年代久远的操作界面一样。也有网友反驳说,国产操作系统界面友好,看起来很舒服。那么事实上是怎样的呢?到底是国产操作系统的设计还停留在人家微软的远古时代,还是部分网友对国产操作系统的认知有偏差?下面我来为大家展示一下。笔者接下来为大家展示的,是在国内做是…

    2022年5月17日
    45
  • oracle分页的方法,oracle分页[通俗易懂]

    oracle分页的方法,oracle分页[通俗易懂]Oracle的oracle分页oracle的分页一共有三种方式方法一根据rowid来分SELECT*FROMEMPWHEREROWIDIN(SELECTRIDFROM(SELECTROWNUMRN,RIDFROM(SELECTROWIDRID,EMPNOFROMEMPORDERBYEMPNODESC)WHEREROWNUM<=((curre…

    2022年5月8日
    212
  • python面试题目及答案(数据库常见面试题及答案)

    Python是目前编程领域最受欢迎的语言。在本文中,我将总结Python面试中最常见的50个问题。每道题都提供参考答案,希望能够帮助你在2019年求职面试中脱颖而出,找到一份高薪工作。这些面试题涉及Python基础知识、Python编程、数据分析以及Python函数库等多个方面。Q1、Python中的列表和元组有什么区别?Q2、Python的主要功能是什么?Python是一种解释型…

    2022年4月17日
    65
  • 单元测试用例概述

    单元测试用例概述测试的覆盖种类       1.语句覆盖:语句覆盖就是设计若干个测试用例,运行被测试程序,使得每一条可执行语句至少执行一次。       2.判定覆盖(也叫分支覆盖):设计若干个测试用例,运行所测程序,使程序中每个判断的取真分支和取假分支至少执行一次。       3.条件覆盖:设计足够的测试用例,运行所测程序,使程序中每个判断的每个条件的每个可能取值至少执行一次。       4.判定—

    2022年6月29日
    30

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号