SpringBoot实战学习(二) SpringMVC搭建项目

1.目录结构

这里写图片描述

2.构建项目

1.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>
  <groupId>com.wen.springmvc4</groupId>
  <artifactId>SpringMVC</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <properties>
      <!-- Generic properties -->
      <java.version>1.8</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>3.1.0</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>

      <!-- 其他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>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>

      <!-- 使用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>

  <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.2</version>
              <configuration>
                  <failOnMissingWebXml>false</failOnMissingWebXml>
              </configuration>
          </plugin>
      </plugins>
  </build>

</project>

2.日志配置
src/main/resource目录下,新建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></jmxConfigurator>
   <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
      <encoder>
         <pattern>logbak: %d{HH:mm:ss.SSS} %logger{36} - %msg%n</pattern>
      </encoder>
   </appender>

   <!--将org.springframework.web包下的类的日志级别设为DEBUG,可看到更详细的错误信息  -->
   <logger name="org.springframework.web" level="DEBUG"></logger>
   <root level="info">
      <appender-ref ref="console"></appender-ref>
   </root>
</configuration>

3.测试页面
src/main/resource目录下,新建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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <pre>Welcome to Spring MVC</pre>
</body>
</html>

4.SpringMVC配置

package com.wen.springmvc4;

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;

/**
 * Spring MVC配置
 * @author EGWri
 *
 */
@Configuration//声明当前类是一个配置类
@EnableWebMvc//开启默认配置,开启SpringMVC支持
@ComponentScan("com.wen.springmvc4")//自动扫描包名下所有使用@Service、@Component、@Repository和@Controller的类,并注册为Bean
public class MyMvcConfig{

    @Bean
    public InternalResourceViewResolver viewResolver(){
        //SpringMVC渲染核心,映射路径和实际页面的位置
        InternalResourceViewResolver viewResolver=new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/classes/views/");//表示路径前缀(运行时目录结构)
        viewResolver.setSuffix(".jsp");//表示路径后缀,假设 ViewName 为 hello,则完整路径为 /WEB-INF/page/hello.jsp
        viewResolver.setViewClass(JstlView.class);//viewClass: 表示要解析的视图类型
        return viewResolver;
    }       
}

5.Web配置

package com.wen.springmvc4;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
/**
 * Web配置
 * @author EGWri
 *
 */
//WebApplicationInitializer是Spring提供用来配置ServLet3.0配置的接口,替代web.xml
//实现此接口会自动被SpringServletContainerInitializer(启动Servlet3.0)获取到
public class WebInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        //新建WebApplicationContext,注册配置类,并将其和当前servletContext关联
        AnnotationConfigWebApplicationContext ctx=new AnnotationConfigWebApplicationContext();
        ctx.register(MyMvcConfig.class);
        ctx.setServletContext(servletContext);
        //注册SpringMVC的DispatcherServlet
        Dynamic servlet=servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);

    }

}

6.控制器

package com.wen.springmvc4.web;

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

@Controller//声明是一个控制器
public class HelloController {

    @RequestMapping("/index")//配置URL和方法之间的映射
    public String hello(){
        System.out.println("HelloController");
        //通过ViewResolver的Bean配置,返回值为index,说明我们的页面放置的路径为/WEB-INF/classes/views/index.jsp
        return "index";
    }
}

3.运行

将程序部署到Tomcat,运行,访问http://localhost:8080/SpringMVC/index
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值