【问题标题】:DispatcherServlet not forwarding my request to the controllerDispatcherServlet 没有将我的请求转发给控制器
【发布时间】:2014-03-15 21:33:55
【问题描述】:

我正在使用 Spring 框架来编写 Web 服务应用程序。应用程序中没有 html 或 jsp 页面。它是纯粹的网络服务。
这是我的控制器类

@RequestMapping("/*")
public class Opinion {


    private FeedbackService fs;

    public Opinion(FeedbackService fs){


        this.fs=fs;
    }


    @RequestMapping(value="/givefeedback",method=RequestMethod.POST)
    public void Login(HttpServletRequest request,HttpServletResponse response) throws IOException, ClassNotFoundException
    {
        ObjectInputStream in=new ObjectInputStream(request.getInputStream());
        serialize.Feedback feedback=(serialize.Feedback)in.readObject();
        fs.writeFeedback(feedback);
        response.setStatus(200);


    }

我的 mvc-config.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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <bean id="poll_Web" class="sef.controller.Opinion">
        <constructor-arg ref="feedbackService" />
        </bean>

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



</beans>

我的 web.xml

<servlet>
        <servlet-name>opinionDispacher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value> classpath:repository-config.xml /WEB-INF/mvc-config.xml 
            </param-value>
        </init-param>
    </servlet>

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

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value> classpath:repository-config.xml /WEB-INF/mvc-config.xml 
        </param-value>
    </context-param>

我正在使用 URL localhost:8080/feedback/givefeedback。该应用程序部署为 feedback.war。但是请求根本没有转发给控制器。我不确定为什么会这样。我也不确定请求进入链中的哪一点。

【问题讨论】:

    标签: java spring spring-mvc jboss


    【解决方案1】:

    你不见了

    <mvc:annotation-driven/>
    

    来自您的 servlet 上下文配置。此元素将发现 @Controller bean 及其 @RequestMapping 方法并将它们注册为处理程序。

    您需要为该命名空间添加适当的 XML 命名空间和架构位置。


    Opinion 设为@Controller 并删除

    @RequestMapping("/*")
    

    它并没有真正起到任何作用。

    【讨论】:

    • 如果我将 Opinios 设为控制器,我会在部署过程中遇到异常,提示“无法创建 bean 意见”
    • @ashwin 在构造函数中添加@Autowired注解。
    【解决方案2】:

    您还在mvc-config.xml 中定义了两次Opinion bean。删除 bean id poll_Web 的 bean 定义。

    【讨论】:

    • Bean Opinion 在 mvc-config.xml 中只定义一次。第二个在哪里?
    • &lt;context:component-scan base-package="sef.controller" /&gt; - 这将扫描包并创建使用注释定义的 bean。例如。 @Controller
    • 你可以找到更多关于组件扫描here
    猜你喜欢
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    相关资源
    最近更新 更多