参考: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 @Configuration7 @ComponentScan8 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 ????@Override14 ????protected Class<?>[] getRootConfigClasses() {15 ????????return new Class<?>[] { RootConfig.class };16 ????}17 18 ????@Override19 ????protected Class<?>[] getServletConfigClasses() {20 ????????System.out.println("------");21 ????????System.out.println("init servlet config classes");22 ????????return new Class<?>[] { WebConfig.class };23 ????}24 25 ????@Override26 ????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 @Configuration13 @EnableWebMvc14 @ComponentScan15 public class WebConfig extends WebMvcConfigurerAdapter {16 17 ????@Bean18 ????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 ????@Override27 ????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项目
原文地址:https://www.cnblogs.com/microcat/p/10199795.html