简体   繁体   中英

Properly configure dispatcher-servlet, applicationContext and web.xml

First time using Spring (and new to MVC architecture as well) and Apache Tomcat and I'm able to load my index just fine after following a few videos/tutorials, but now that I'm trying to add other pages, I keep getting a 404 error. Here's what I have so far and the project structure is below as well for reference. The mapping of resources also seems to not be working as my index isn't loading 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";
    }
}

项目结构

You're missing a url-pattern value in your web.xml. It is empty.

Just change it to: <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM