IDEA--IDEA配置web项目

时间:2018-12-30 16:46:20   收藏:0   阅读:273

参考: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>

 

具体部署位置:

技术分享图片

 

原文:https://www.cnblogs.com/microcat/p/10199795.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!