剛開始用idea構建spring mvc項目,代碼沒問題都是跟着書敲,但是配置真的是一言難盡,網上有些教程,但idea的比較少,對我這種菜鳥不友好,在自己遇到很多坑後,終於解決了,記錄下。。
首先電腦上要裝有tomcat來發布web項目,版本在7以上,爲了兼容servelet3.0
從新建項目開始:
自己選擇項目路徑,等待下載庫
接着按照書上的代碼將各個類建好,我的目錄結構是這樣的
之後,將項目添加到tomcat中
修改完後點擊ok,tomcat配置完畢,哈哈
嘗試運行下tomcat,結果報錯。。
我們添加jar包來解決這個問題。
找到tomcat安裝目錄lib下的,點擊添加。添加完成後再次運行tomcat這個問題應該解決了。但是錯誤並未都解決,我們打開tomcat localhost log控制檯,應該會有下面的錯誤。
再次嘗試啓動後又出現下面的問題,心好累。。
查了半天才發現,按照書上的方法是通過java類的方式來指定servelet容器,映射規則等,打開web.xml,就會發現裏面的三條配置項和java類,就是SpittrWebApplicationInitializer,裏是一一對應的。兩種配置產生了衝突,選擇註釋web.xml
再次運行程序發現打開的是index.jsp,但始終還是打開了,所有控制檯的日誌都沒報錯就好哈哈。
接下來就是修改映射的問題。我不知道爲什麼代碼裏已經有將"/"的get請求映射到home.jsp下,但是就是找不到資源,心好累,希望知道的大佬評論區指點下,是不是tomcat那裏的路徑有問題,搞了好久。。繼續說我是怎麼解決的,我修改了類的映射,原來書中是這樣的:
如果報http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar錯,就下載對應的jar包就好,參考這裏
http://www.runoob.com/jsp/jsp-jstl.html,把那兩個jar拷到tomcat下應該就可以了
這時候我們重新啓動程序,並在地址欄中輸入home
我還是把代碼貼一下吧,可以對比下:
package spittr.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan(basePackages={"spittr"},
excludeFilters={@Filter(type= FilterType.ANNOTATION, value= EnableWebMvc.class)})
public class RootConfig {
}
package spittr.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class SpitterWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
/**
* 將一個或多個路徑映射到DispatcherServlet上
*/
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { RootConfig.class };
}
/**
* 指定配置類
* 當DispatcherServlet啓動的時候,它會創建Spring應用上下文,並加載配置類或配置文件中所聲明的bean
*/
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebConfig.class };
}
}
package spittr.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 //啓用Spring Mvc
@ComponentScan("spittr.web")
public class WebConfig extends WebMvcConfigurerAdapter {
/**
* 配置視圖解析器,名爲home的視圖將被解析爲/WEB-INF/views/home.jsp
* @return
*/
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
/**
* 配置靜態資源的處理
* 此配置要求DispatcherServlet將對靜態資源的請求轉發到Servlet容器中默認的Servlet上,
* 而非使用DispatcherServlet本身來處理此類請求
*/
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
// TODO Auto-generated method stub
//super.configureDefaultServletHandling(configurer);
configurer.enable();
}
}
package spittr.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller //聲明一個控制器
public class HomeController {
@RequestMapping(value="/home", method=RequestMethod.GET) //處理對"/"的GET請求
public String home() {
System.out.println("處理get請求");
return "home";
}
}