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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • centos7安装python3.7_python软件安装步骤

    centos7安装python3.7_python软件安装步骤Centos7安装Python3.8详细教程Centos7安装Python3.8详细教程安装编译相关工具下载python安装包编译安装python创建软连接更改yum配置Centos7安装Python3.8详细教程安装编译相关工具yum-ygroupinstall”Developmenttools”yum-yinstallzlib-develbzip2-developenssl-develncurses-develsqlite-develreadline-develtk-d

    2022年9月24日
    1
  • 全局钩子注入

    全局钩子注入全局钩子介绍hook,指利用api来提前拦截并处理windows消息的一种技术。如键盘钩子,许多木马都有这东西,监视你的键盘操作。全局钩子是系统钩子的一种,当指定的一些消息被系统中任

    2021年12月13日
    44
  • qt将毫秒级时间戳转换为日期(js把对象变成字符串)

    需要加入的头文件:#include<QTime>一.秒单位时间转为字符串时、分、秒格式输出inttime_sec=100;QStringtimer=QTime(0,0,0).addSecs(int(time_sec)).toString(QString::fromLatin1(“HH:mm:ss”));qDebug()<<timer;//输出:”00:01:40″二.秒单位时间转为字符串时、分、秒、毫秒格式输出..

    2022年4月12日
    577
  • dreamweaver cs6 html教程,Dreamweaver cs6安装详细图文教程

    dreamweaver cs6 html教程,Dreamweaver cs6安装详细图文教程类型:Mac应用软件大小:314.6M语言:中文评分:10.0标签:立即下载Dreamweaver这款强大的所见即所得的网页编辑器相信大家都有用过,CS6这个新版本增加了对Html5、css及jqurey的支持,还有其他一些功能的增加。不过建议新手是没必要下这个版本的,毕竟这个版本的功能对于刚接触DW的人来说用处不是很大,用CS5足矣。西西为大家制作了Dreamweavercs6的详细安装图文…

    2022年5月18日
    39
  • 1.两数之和-Python-LeetCode

    1.两数之和-Python-LeetCode刚开始接触算法方面,好多都不懂,打算每刷一题就整理一下题目:给定一个整数数列,找出其中和为特定值的那两个数。你可以假设每个输入都只会有一种答案,同样的元素不能被重用。示例:给定nums=[2,7,11,15],target=9因为nums[0]+nums[1]=2+7=9所以返回[0,1]解法一:.刚开始看到的的时候,第一个想到的就是用一个嵌套循环把n…

    2022年6月8日
    26
  • RewriteCond 详解「建议收藏」

    RewriteCond 详解「建议收藏」RewriteCond重写规则执行条件语法:RewriteCondTestStringCondPattern生效域:serverconfig,virtualhost,directory,.htaccess特别的上面的TestString,可提供反向引用.引用模式为:%N其中N为(0&lt;=N&lt;=9),引用当前若干Rew…

    2022年6月14日
    25

发表回复

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

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