IDEA18+jdk1.8+tomcat9.0+spring 4.0

项目搭建:

1. 目录

2. 跳转页面:http://localhost:8080/mychapter14_war_exploded/my/index/

本地:8080/Deployment+ResultMapping("")+web.xml中的<servlet-mapping>中的<url-pettern>/<>

3. 代码:

dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <mvc:annotation-driven/>
    <context:component-scan base-package="Controller"/>
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>


</beans>

MyController.java

package Controller;

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

import java.util.logging.Logger;


@Controller("myController")
@RequestMapping("/my")
public class MyController {
    @RequestMapping("/index")
    public ModelAndView index(){
        Logger logger= (Logger) Logger.getLogger(String.valueOf(MyController.class));
        ModelAndView modelAndView=new ModelAndView();

        modelAndView.setViewName("index");
        return modelAndView;
    }
}

4:曾经的errors

4.1 404找不到XXX或者

java.io.FileNotFoundException: Could not open ServletContext resource

就是tomcat配置问题,tomcat找不到你的application.xml等。

4.1.1我的问题是乱删乱改。导致如下红框没有了。

别不会不知道就瞎删,累不死你。

 4.1.2找不到mychapter14_war_exploded/JSP/index.jsp/

因为之前目录是JSP,所以在我重命名之后为jsp,但是out里面还没改,所以导致controller找到的是web/index/jsp文件,而不是web/WEB-INF/jsp/index.jsp(也就是web.xml中的设置)。将out的JSP改为jsp就可以了。

后话:改了好几天的错。结果今早重新新建了spring web项目就给好了。无语无语。还瞎改project structure了。

不能照着书上的抄,要理解自己写,才能修改,就不会在改半天了