注解方式构建一JAVA Spring MVC 项目结构

注解方式构建一JAVA Spring MVC 项目结构

  • 步骤概览

    • 新建maven项目【war包形式】
    • 添加pom依赖
    • 新建一个普通的java类,WebInitializer,实现接口WebApplicationInitializer,重写onStartup()方法。
      这个类相当于是原来文件中的Web.xml
    • 新建一个普通的java类,实现WebApplicationInitializer接口。这个类就相当于传统结构的Spring MVC的配置文件。
    • 编写对应的Controller 路由和视图模板。
    • 测试是否能成功访问。
  • 新建项目

    • 新建一个maven项目,打包的方式为war包。
      pom文件如下:【省略文件头标签】
    <properties>
        
        <java.version>1.7java.version>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        
        <jsp.version>2.2jsp.version>
        <jstl.version>1.2jstl.version>
        <servlet.version>3.1.0servlet.version>
        
        <spring-framework.version>4.1.5.RELEASEspring-framework.version>
        
        <logback.version>1.0.13logback.version>
        <slf4j.version>1.7.5slf4j.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>javaxgroupId>
            <artifactId>javaee-web-apiartifactId>
            <version>7.0version>
            <scope>providedscope>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>${spring-framework.version}version>
        dependency>

        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
            <version>${jstl.version}version>
        dependency>

        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>${servlet.version}version>
            <scope>providedscope>
        dependency>

        <dependency>
            <groupId>javax.servlet.jspgroupId>
            <artifactId>jsp-apiartifactId>
            <version>${jsp.version}version>
            <scope>providedscope>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-txartifactId>
            <version>${spring-framework.version}version>
        dependency>

        
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
            <version>${slf4j.version}version>
        dependency>
        <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.16version>
        dependency>
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>jcl-over-slf4jartifactId>
            <version>${slf4j.version}version>
        dependency>
        <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-classicartifactId>
            <version>${logback.version}version>
        dependency>
        <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-coreartifactId>
            <version>${logback.version}version>
        dependency>
        <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-accessartifactId>
            <version>${logback.version}version>
        dependency>

        
        
        
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.5.3version>
        dependency>

        
        <dependency>
            <groupId>commons-fileuploadgroupId>
            <artifactId>commons-fileuploadartifactId>
            <version>1.3.1version>
        dependency>
        
        <dependency>
            <groupId>commons-iogroupId>
            <artifactId>commons-ioartifactId>
            <version>2.3version>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>${spring-framework.version}version>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.11version>
            <scope>testscope>
        dependency>

    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>2.3.2version>
                <configuration>
                    <source>${java.version}source>
                    <target>${java.version}target>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-war-pluginartifactId>
                <version>2.3version>
                <configuration>
                    <failOnMissingWebXml>falsefailOnMissingWebXml>
                configuration>
            plugin>
        plugins>
    build>
  • 新建web工程初始化类。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

/**
 * @Type MyMvcConfig.java
 * @Desc 
 * @author duanq
 * @date 2018年4月30日 下午3:07:18
 * @version 
 */
// 开启配置
@Configuration
// 开启WebMvc
@EnableWebMvc
// 包扫描的位置,需要覆盖所有的子包
@ComponentScan("com.test.mvc")
public class MyMvcConfig extends WebMvcConfigurerAdapter {

    /**
     * 注入视图解析器
     * @return
     */
    @Bean
    public InternalResourceViewResolver  viewResolver () {

        InternalResourceViewResolver  resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/classes/views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);

        return resolver;
    }   
}
  • 新建SpringMVC的控制文件:
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;

/**
 * @Type WebInitializer.java
 * @Desc 相当于是web.xml中配置的参数
 * @author duanq
 * @date 2018年4月30日 下午3:11:57
 * @version 
 */

public class WebInitializer implements WebApplicationInitializer{


    public void onStartup(ServletContext servletContext) throws ServletException {
       AnnotationConfigWebApplicationContext  atx = new AnnotationConfigWebApplicationContext();
       atx.register(MyMvcConfig.class);
       atx.setServletContext(servletContext);

       Dynamic addServlet = servletContext.addServlet("dispatcher", new DispatcherServlet(atx));
       addServlet.addMapping("/");
       addServlet.setLoadOnStartup(1);
       // 开启对异步任务的支持
       addServlet.setAsyncSupported(true);
    }

}
  • 编写对应的Controller和对应的视图模板
  import javax.servlet.http.HttpServletRequest;

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

  import com.mvc.entity.Customer;

  /**
   * @Type IndexController.java
   * @Desc
   * @author duanq
   * @date 2018年4月30日 下午3:16:55
   * @version
   */
  @Controller
  public class IndexController {

      @RequestMapping("/")
      public String gotoIndex() {
          System.out.println("action action ");
          return "index";
      }

      @RequestMapping("/jsonTest")
      @ResponseBody
      public Customer testGetJson() {
          Customer customer = new Customer();
          customer.setId(1);
          customer.setName("张三");
          customer.setPhone("17076265904");
          customer.setSex("男");
          customer.setAddress("北京市海淀区");
          return  customer;
      }

  }

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title heretitle>
head>
<body>
        <h2>welcome indexh2>
body>
html>
  • 测试访问:http://localhost:8083/test2/

你可能感兴趣的:(注解方式构建一JAVA Spring MVC 项目结构)