IDEA--IDEA配置web项目

Posted MicroCat

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IDEA--IDEA配置web项目相关的知识,希望对你有一定的参考价值。

参考:https://blog.csdn.net/kfm1376822651/article/details/79666586

 

记学习springmvc时,使用idea部署web项目至tomcat.

新建模块springmvc(spring mvc项目)

 

将springmvc模块置于maven下管理

 

添加相关依赖

 

配置DispatcherServerlet,添加一个controller及其jsp

 

 

配置文件

删除自动生成的相关配置,这里使用的是java config方式来配置DispatcherServlet及其他配置.

 

 配置artifacts

 

配置Tomcat

 

运行

 

OJBK

 

 

相关代码,来源于spring action -- spring mvc.

1 package org.wzh.three2.springmvc;
2 
3 import org.springframework.context.annotation.ComponentScan;
4 import org.springframework.context.annotation.Configuration;
5 
6 @Configuration
7 @ComponentScan
8 public class RootConfig {
9 }
 1 package org.wzh.three2.springmvc;
 2 
 3 import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
 4 
 5 public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
 6 
 7     static {
 8         System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
 9         System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
10         System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
11     }
12 
13     @Override
14     protected Class<?>[] getRootConfigClasses() {
15         return new Class<?>[] { RootConfig.class };
16     }
17 
18     @Override
19     protected Class<?>[] getServletConfigClasses() {
20         System.out.println("------");
21         System.out.println("init servlet config classes");
22         return new Class<?>[] { WebConfig.class };
23     }
24 
25     @Override
26     protected String[] getServletMappings() {
27         return new String[] { "/" };
28     }
29 }
 1 package org.wzh.three2.springmvc;
 2 
 3 import org.springframework.context.annotation.Bean;
 4 import org.springframework.context.annotation.ComponentScan;
 5 import org.springframework.context.annotation.Configuration;
 6 import org.springframework.web.servlet.ViewResolver;
 7 import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
 8 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 9 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
10 import org.springframework.web.servlet.view.InternalResourceViewResolver;
11 
12 @Configuration
13 @EnableWebMvc
14 @ComponentScan
15 public class WebConfig extends WebMvcConfigurerAdapter {
16 
17     @Bean
18     public ViewResolver viewResolver() {
19         InternalResourceViewResolver resolver = new InternalResourceViewResolver();
20         resolver.setPrefix("/WEB-INF/views/");
21         resolver.setSuffix(".jsp");
22         resolver.setExposeContextBeansAsAttributes(true);
23         return resolver;
24     }
25 
26     @Override
27     public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
28         configurer.enable();
29     }
30 }
 1 package org.wzh.three2.springmvc.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RequestMethod;
 6 
 7 @Controller
 8 public class HomeController {
 9 
10     @RequestMapping(value = "/home", method = RequestMethod.GET)
11     public String home() {
12         System.out.println("ENTER HOMECONTROLLER..");
13         return "home";
14     }
15 
16 }
 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: Pear
 4   Date: 2018/12/25
 5   Time: 21:53
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 <html>
10   <head>
11     <title>$Title$</title>
12   </head>
13   <body>
14   $END$
15   </body>
16 </html>

 

具体部署位置:

 

以上是关于IDEA--IDEA配置web项目的主要内容,如果未能解决你的问题,请参考以下文章

IntelliJ IDEA :IDEA常用配置

idea启动build过慢

idea的常用设置

Linux 环境 Intelij Idea 安装与快捷图标配置

IDEA:idea快速修改git仓库地址

[Idea] idea打不开项目,原因很莫名