为什么我通过BUILD SUCCESS获得此错误?

时间:2018-02-16 03:27:54

标签: java spring spring-mvc routing spring-annotations

我使用的是spring-5.0.3

http://localhost:8080/firstapp_spring/greeting

HTTP Status 500 - Internal Server Error

type Exception report

messageInternal Server Error

description The server encountered an internal error that prevented it from fulfilling this request.

exception
javax.servlet.ServletException: No adapter for handler [com.actions.GreetingController@4275cbe5]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler

构建项目期间没有错误。

pom.xml =>

  <dependencies>    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.0.3.RELEASE</version>
    </dependency>


<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
</dependency>

<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.23</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.0.3.RELEASE</version>
</dependency>   

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>5.0.3.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.0.3.RELEASE</version>
</dependency>

类=&GT;

package com.actions;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class GreetingController {

    @RequestMapping(value = "/firstapp_spring/greeting", method = RequestMethod.GET)
    public String greetingForm(Model model) {
        model.addAttribute("greeting", new Greeting());
        return "greeting";
    }

    @RequestMapping(value = "/firstapp_spring/greeting", method = RequestMethod.POST)
    public String greetingSubmit(@ModelAttribute Greeting greeting) {
        return "result";
    }

}

APP-servlet.xml中=&GT;

<?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-3.0.xsd">

<!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> -->

    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
      <property name="templateLoaderPath" value="/"/>
    </bean>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="prefix">
            <value>/</value>
        </property>
        <property name="suffix">
            <value>.ftl</value>
        </property>
    </bean>

    <bean name="/hello" class="com.actions.HelloController"></bean>

    <bean name="/greeting" class="com.actions.GreetingController"></bean>

</beans>

1 个答案:

答案 0 :(得分:1)

您在Controller类中定义的请求映射是:

@RequestMapping(value = "/firstapp_spring/greeting", method = RequestMethod.GET)

您尝试访问的网址是:

http://localhost:8080/myproject/greeting

你错过了"/firstapp_spring"部分。

因为你正在使用Spring&gt; 4.3您可以利用新的HTTP映射注释:@GetMapping@PostMapping

您的请求映射可以变得更短,等同于:

@GetMapping("/firstapp_spring/greeting")