spring web mvc环境搭建

这两天在学习spring,哎,没办法呀,成都基本都是java,不喜欢学这个也没有用。多学点多条路。还得生活不是。

好了,言归正传,我这里记录下我搭建spring mvc的过程,其实过程不算太难,主要是开始我用的xml配置,后面改成java配置的(现在好像都倾向于java代码配置了),不知道什么时候丢了tomcat的配置,然后一直找不到controller,让我查了好几天。

搭建之前,需要安装maven且配置正确,我这里也没用的xml配置,用的java的配置。

一 创建spring mvc工程,我是基本maven骨架弄的。选的webapp这个。

二 配置pom.xml,增加一些依赖

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.source>1.7</maven.compiler.source>
  <maven.compiler.target>1.7</maven.compiler.target>
  <spring.version>4.3.18.RELEASE</spring.version>
  <spring.jdbc.version>5.2.0.RELEASE</spring.jdbc.version>
  <mysql.connector.version>5.1.21</mysql.connector.version>
  <servlet.version>3.0.1</servlet.version>
  <jsp.version>2.2.1-b03</jsp.version>
  <jstl.version>1.2</jstl.version>
</properties>

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
  </dependency>

    <dependency>
        <groupId>javax.servlet.jsp.jstl</groupId>
        <artifactId>jstl-api</artifactId>
        <version>1.2</version>
    </dependency>

   <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>${servlet.version}</version>
  </dependency>

  <dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>${jsp.version}</version>
    <scope>provided</scope>
  </dependency>
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>${jstl.version}</version>
  </dependency>

  <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
  </dependency>

  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>${spring.version}</version>
  </dependency>

  <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>${spring.version}</version>
    <scope>test</scope>
  </dependency>


  <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
  </dependency>

  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${spring.version}</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
  </dependency>

<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring4 -->
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring4</artifactId>
    <version>3.0.11.RELEASE</version>
</dependency>

  <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${spring.jdbc.version}</version>
  </dependency>
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>${mysql.connector.version}</version>
  </dependency>

</dependencies>

三 创建项目目录结构,如下图

配置DispatcherServlet

初始化,继承AbstractAnnotationConfigDispatcherServletInitializer这个类,spring会自动检测到的。

package com.smartrui.common.config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    private static final String CHARACTER_ENCODING = "UTF-8";

    private static final String DISPATCHER_SERVLET_MAPPING = "/";

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{WebAppConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{DISPATCHER_SERVLET_MAPPING};
    }

    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding(CHARACTER_ENCODING);
        characterEncodingFilter.setForceEncoding(true);
        return new Filter[] { characterEncodingFilter };
    }
}

增加配置文件,需要添加几个注解 ,@EnableWebMvc, @Configuration,@ComponentScan,其中ComponentScan这里增加需要让spring注入的包。

package com.smartrui.common.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.smartrui.dao","com.smartrui.service","com.smartrui.controller"})
public class WebAppConfig extends WebMvcConfigurerAdapter {
    
    @Override
    public void addResourceHandlers( ResourceHandlerRegistry registry) {
        super.addResourceHandlers(registry);
        registry.addResourceHandler("/img/**").addResourceLocations("/img/");
      
    }

    
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }


    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }


}

五 写一个controller入口

package com.smartrui.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/home")
public class HomeController {

    @RequestMapping("/hello")
    public String hello(Model model){
        List<String> s = new ArrayList<>();
        s.add("1");
        s.add("2");
        model.addAttribute("mystr", s);
        return "hello"; //返回视图名 web-info/views/hello.jsp
    }

    @RequestMapping(value="/user/{id}",method= RequestMethod.GET)
    public String show(@PathVariable("id") long id,Model model) {
        //接受参数测试  /home/user/33
        model.addAttribute("id",id);
        return "hello";
    }

    @RequestMapping("/welcome")
    public String welcome(){

        return "welcome"; //返回视图名 web-info/views/welcome.html
    }

}

六 配置 tomcat

这里那个Deploy at the server startup这里要注意配置下,我开始从xml配置换成java配置的时候,这里配置没了。然后就一直找不到controller。

配置完后,点运行,如果没问题,在浏览器输入 http://localhost:8080/home/hello 应该就能显示了.

这里只是一个基本的环境配置,像数据库,模板什么的都没有配置,这些后续慢慢配置吧.

posted @ 2019-10-16 17:15  随彦心MO  阅读(986)  评论(0编辑  收藏  举报