温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在IDEA中搭建最小可用SpringMVC项目

发布时间:2021-02-07 11:49:40 来源:亿速云 阅读:169 作者:小新 栏目:编程语言

小编给大家分享一下如何在IDEA中搭建最小可用SpringMVC项目,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

1. 新建 SpringMVC 的 Web 项目

  • File - New - Project..

  • 勾选 SpringMVC 和 WebApplication ,点击Next

  • 填写 Project name : hello

  • 点击 Finish

  • IDEA 会自动下载所需的 SpringMVC 的 jar 包

2. 代码编写

代码参考 《Spring 实战》(第四版),本文和书中代码略有差异

删除不需要的配置文件

  • 删除 WEB-INF 下的 web.xml

  • 删除 WEB-INF 下的 dispatcher-servlet.xml

  • 删除 WEB-INF 下的 applicationContext.xml

  • 删除 web 下的 index.jsp

编写 JavaConfig 文件

  • 新建 package : com.yangrd.springmvc.config

  • 新建 配置文件 HelloWebAppInitializer.java

package com.yangrd.springmvc.config;

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

public class HelloWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  @Override
  protected Class<?>[] getRootConfigClasses() {
    System.out.println("getRootConfig");
    return new Class<?>[]{RootConfig.class};
  }

  @Override
  protected Class<?>[] getServletConfigClasses() {
    System.out.println("getServletConfig");
    return new Class<?> []{WebConfig.class};
  }

  @Override
  protected String[] getServletMappings() {
    System.out.println("getServletMappings");
    return new String[]{"/"};
  }
}

新建配置文件 RootConfig.java

package com.yangrd.springmvc.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan(basePackages = {"com.yangrd.springmvc"},
    excludeFilters = {@ComponentScan.Filter(type= FilterType.ANNOTATION,value = EnableWebMvc.class)})
public class RootConfig {
}

新建配置文件 WebConfig.java

package com.yangrd.springmvc.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan("com.yangrd.springmvc.controller")
public class WebConfig extends WebMvcConfigurerAdapter {
  @Bean
  public ViewResolver viewResolver(){
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".html");
    resolver.setExposeContextBeansAsAttributes(true);
    return resolver;
  }
  @Override
  public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){
    configurer.enable();
  }
}

编写 Controller

  • 新建 package : com.yangrd.springmvc.controller

  • 新建 文件 HelloController.java

package com.yangrd.springmvc.controller;

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

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 *
 */
@Controller //声明为一个控制器
public class HelloController {
  @RequestMapping(value = "/home",method = GET)//处理对 “/” 的 GET 请求
  public String hello(){
    return "hello"; //逻辑视图名为hello
  }
}

编写 view 文件

  • 在 WEB-INF 下新建文件夹 views

  • 在 views 文件夹下新建 hello.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Hello</title>
</head>
<body>
  hello world
</body>
</html>

3. Tomcat 的配置和启动

配置tomcat服务

  • 点击 IDEA 右上角 绿色的小锤子图标旁的 Add Configuration...

  • 在弹出页面中,点击加号

  • 选择 Tomcat Server - Local

  • 填写 Name : helloServer

  • 点击 Deployment - 点击 + ,选择 Artifact

  • 点击 Apply, OK

将Sping MVC 相关包放到 Web 工程 中的 lib 下

  • File - Project Structure...

  • 选择 Artifacts

  • 在右侧的 Available Elements 中 hello 下 有两个 Spring 的jar上,右键 选择 `Put into /WEB-INF/lib

  • 点击 Apply - OK

启动tomcat

这是启动tomcat 会报错

Error:(5, 8) java: 无法访问javax.servlet.ServletException
  找不到javax.servlet.ServletException的类文件

这时需要添加 javax.servlet-api

4. 测试

浏览器访问 http://localhost:8080/home

显示

hello world

以上是“如何在IDEA中搭建最小可用SpringMVC项目”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI