1. 程式人生 > >IDEA 通過Maven創建Spring MVC項目搭建

IDEA 通過Maven創建Spring MVC項目搭建

javax cor 內容 imp ram als report spa 使用

概述

  本篇隨筆主要記錄內容如下:

1、通過Maven創建基於Spring Framework類庫的MVC項目,免去了繁瑣的XML配置;

2、在Idea裏面配置Tomcat的測試啟動項;

Maven創建MVC項目

  2.1、新建Maven項目:New Project-->Maven

技術分享圖片

  2.2、修改配置

配置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.wisely</groupId> <artifactId>hightlight_springmvc4</artifactId> <version>1.0
-SNAPSHOT</version> <packaging>war</packaging> <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>3.1.0</servlet.version> <!--spring--> <spring-framwork.version>4.1.5.RELEASE</spring-framwork.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-framwork.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-framwork.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.3</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project>

  在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>logbak: %d{HH:mm:ss.SSS} %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="org.springframework.web" level="DEBUG"/> <!-- 1 -->
    <root level="info">
        <appender-ref ref="console"/>
    </root>
</configuration>

  在src/main/resources新建views文件夾,在src/main/resources/views新建index.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>首頁</title>
</head>
<body>
    <pr>
        Welcome to Spring Mvc World
    </pr>
</body>
</html>

  PS(右鍵views新建的時候並沒有jsp類型,需要將views設置為web路徑。Project Structure-->Modules-->選擇web,設置Web Resource Directory)

技術分享圖片

  2.3、新建代碼塊

新建MvcConfig文件,用於配置MVC的映射規則,主要使用的InternalResourceViewResolver 類;PS:通過Package,查找新建的類的路徑

package com.wisely.highlight_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;

@Configuration
@EnableWebMvc
@ComponentScan("com.wisely.highlight_springmvc4")
public class MyMVCConfig {

    @Bean
    public InternalResourceViewResolver viewResolver(){
        InternalResourceViewResolver viewResolver=new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/classes/views/");
        viewResolver.setSuffix(".jsp");
        viewResolver.setViewClass(JstlView.class);
        return  viewResolver;
    }
}

   新建WebInitializer繼承WebApplicationInitializer,重寫裏面OnStartUp方法。在WEB中,我們使用AnnotationConfigWebApplicationContext,代碼如下

package com.wisely.highlight_springmvc4;

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.Dynamic;

public class WebInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext context=new AnnotationConfigWebApplicationContext();
        context.register(MyMVCConfig.class);
        context.setServletContext(servletContext);

        Dynamic servlet=servletContext.addServlet("dispatcher",new DispatcherServlet(context));
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);

    }
}

  新建HelloController

package com.wisely.highlight_springmvc4.web;

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

@Controller
public class HelloController {

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

Idea裏面配置Tomcat

  3.1、選擇Edit Configration,設置Tomcat

技術分享圖片

  3.2、設置Deployment,選擇"+"之後,選擇Artifacts

技術分享圖片

PS:war模式—-將WEB工程以包的形式上傳到服務器 ;war exploded模式—-將WEB工程以當前文件夾的位置關系上傳到服務器

  3.3、設置Server內容項

技術分享圖片

啟動項目RunTomcat即可,輸入 http://localhost:8009/H99/即可訪問頁面信息

技術分享圖片

下載Demo

IDEA 通過Maven創建Spring MVC項目搭建