在SpringMVC Controller中获得404

时间:2016-12-02 09:17:35

标签: java spring maven spring-mvc

我创建了一个maven项目(实际上它是用spring,spring数据制作的)。它的措辞很好,但我面临的问题是,当我尝试通过url使用控制器服务时,我收到404错误。

以下是网址:http://localhost:8080/clt/larw/verify?signature=signature&timestamp=timestamp&nonce=nonce&echostr=echostr

但是jsp工作得很好:http://localhost:8080/clt/

的web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      version="3.0">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:/WEB-INF/config/sprint-root.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>  

</web-app>

弹簧servlet.xml中

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

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <!-- Only needed because we install custom converters to support the examples in the org.springframewok.samples.mvc.convert package -->
    <!-- <beans:bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <beans:property name="formatters">
            <beans:bean class="org.springframework.samples.mvc.convert.MaskFormatAnnotationFormatterFactory" />
        </beans:property>
    </beans:bean> -->

    <!-- Only needed because we require fileupload in the org.springframework.samples.mvc.fileupload package -->
    <!-- beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /> -->

    <!-- Imports user-defined @Controller beans that process client requests -->
    <beans:import resource="spring-controllers.xml" />

    <!-- <task:annotation-driven /> -->

</beans:beans>

弹簧controller.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- Maps '/' requests to the 'home' view -->
    <mvc:view-controller path="/" view-name="index"/>

    <mvc:annotation-driven />

    <context:component-scan base-package="org.clt.controller" />

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
</beans>

spring-root 为空。

控制器:

package org.clt.controller;

import org.clt.service.WechatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/larw")
public class WechatListener {

    @Autowired
    private WechatService wechatService;

    @RequestMapping(value="/verify", method=RequestMethod.GET)
    public String verification(@RequestParam String signature,@RequestParam String timestamp,
            @RequestParam String nonce, @RequestParam String echostr) {
        System.out.println("-----------------come in WechatListener-----------------");

        String result = null;
        Boolean flag = wechatService.verifyWechatConfig(signature, timestamp, nonce);

        return flag ? echostr : result;
    }
}

项目: project.jpg

任何人都可以告诉我一些解决方案,谢谢。

1 个答案:

答案 0 :(得分:0)

我看到的第一件事是 web.xml 中存在拼写错误 <param-value>classpath*:/WEB-INF/config/sprint-root.xml</param-value> 尝试更改 sprint -root.xml - &gt;弹簧的ROOT.xml