正确配置调度程序servlet,applicationContext和web.xml

时间:2018-10-21 02:58:21

标签: java spring-mvc tomcat intellij-idea

第一次使用Spring(也是MVC架构中的新功能)和Apache Tomcat,并且在学习了一些视频/教程之后,我能够很好地加载索引,但是现在我试图添加其他页面,我不断收到404错误。这是我到目前为止的内容,下面也提供了项目结构以供参考。资源的映射似乎也不起作用,因为我的索引没有加载images / js / etc。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern></url-pattern>
    </servlet-mapping>
</web-app>

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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven/>
    <context:component-scan base-package="org.hackathon_10_20.EasySub.controller"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <mvc:resources mapping="/resources/**" location="/WEB-INF/theme/startbootstrap-new-age-gh-pages/" cache-period="31556926"/>
</beans>

applicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>

IndexController.java

package org.hackathon_10_20.EasySub.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * Created by Chris Bonilla on 10/20/2018.
 */

@Controller
public class IndexController {
    @GetMapping("/")
    public String index(Model m) {
        m.addAttribute("someAttribute", "someValue");
        return "index";
    }
}

SubPlanController.java

package org.hackathon_10_20.EasySub.controller;

import org.hackathon_10_20.EasySub.domain.SubPlan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.util.Map;
import java.util.HashMap;


@Controller
public class SubPlanController {

    Map<Long, SubPlan> subPlanMap = new HashMap<>();

    @RequestMapping(value ="/subplan", method = RequestMethod.GET)
    public ModelAndView showForm() {
        return new ModelAndView("subPlanHome", "SubPlan", new SubPlan());
    }

    @RequestMapping(value = "/subplan/{Id}", produces = { "application/json", "application/xml"}, method = RequestMethod.GET)
    public @ResponseBody SubPlan getSubPlanById(@PathVariable final long Id) {
        return subPlanMap.get(Id);
    }

    @RequestMapping(value = "/addSubPlan", method = RequestMethod.POST)
    public String submit(@ModelAttribute("SubPlan") final SubPlan subplan, final BindingResult result, final ModelMap model) {
        if (result.hasErrors()) {
            return "error";
        }
        //model.addAttribute("name", subplan.getName());
        //subPlanMap.put(subplan.getId(), subplan);
        return "subPlanView";
    }
}

link

1 个答案:

答案 0 :(得分:1)

您在web.xml中缺少url-pattern值。它是空的。

只需将其更改为:      <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>

相关问题