在练习册中从春天开始简单应用的错误

时间:2014-12-21 07:48:30

标签: java spring

引起: org.eclipse.jetty.servlet.ServletHolder $ 1:org.springframework.beans.factory.BeanCreationException:使用名称' org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0'创建bean时出错。 :bean的初始化失败;嵌套异常是java.lang.IllegalStateException:找到了不明确的映射。无法映射&rosterController'豆方法 public void com.springinpractice.ch03.web.RosterController.list(org.springframework.ui.Model) to {[],methods = [],params = [],headers = [],consume = [],produce = [],custom = []}:已有' rosterController'豆方法 public void com.springinpractice.ch03.web.RosterController.member(java.lang.Integer,org.springframework.ui.Model)mapping。

main-servlet.xml

        <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
        http://www.springframework.org/schema/jee
        http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        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">

    <context:component-scan base-package="com.springinpractice.ch03.web"/>
    <context:annotation-config />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
 p:prefix="/WEB-INF/jsp/roster/" p:suffix=".jsp"/>
</beans>

RosterController.java

roasterController

     /* 
 * Copyright (c) 2013 Manning Publications Co.
 * 
 * Book: http://manning.com/wheeler/
 * Blog: http://springinpractice.com/
 * Code: https://github.com/springinpractice
 */
package com.springinpractice.ch03.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.springinpractice.ch03.model.Member;

@Controller
public final class RosterController {
    private List<Member> members = new ArrayList<Member>();

    public RosterController() {
        members.add(new Member("John", "Lennon"));
        members.add(new Member("Paul", "McCartney"));
        members.add(new Member("George", "Harrison"));
        members.add(new Member("Ringo", "Starr"));
    }


    @RequestMapping("/")
    public String list(Model model) {
    model.addAttribute("memberList",members); 
    return "list"; } 



    @RequestMapping("/member") 
    public String member(@RequestParam("id") Integer id, Model model) { 
        model.addAttribute(members.get(id));return "member"; }
}

我得到了

循环视图路径[]:将再次调度回当前处理程序URL [/ sip /]。检查您的ViewResolver设置! (提示:由于默认的视图名称生成,这可能是未指定视图的结果。)

我的观点

<%-- Source project: sip03, branch: 01 (Maven Project) --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
    <head>
        <title>Roster</title>
    </head>
    <body>
        <h1>Roster</h1>
        <ul>
            <c:forEach var="member" items="${memberList}" 
                 varStatus="status">
                <li>
                    <a href="member.do?id=${status.index}">
                        <c:out value="${member}"></c:out>
                    </a>
                </li>
            </c:forEach>
        </ul>
    </body>
</html>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Source project: sip03, branch: 01 (Maven Project) -->

<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_2_5.xsd"
    version="2.5">

    <servlet>
        <servlet-name>main</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>main</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

1 个答案:

答案 0 :(得分:0)

在您的main-servlet.xml文件中缺少行,这是激活注释所必需的。

<context:annotation-config />

更改您的main-servlet.xml文件,如下所示 - &gt;

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
        http://www.springframework.org/schema/jee
        http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        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">

    <context:component-scan base-package="com.springinpractice.ch03.web."/>
    <context:annotation-config />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/jsp/roster/"
        p:suffix=".jsp"/>
</beans>

并更改您的列表方法,如下所示。

 @RequestMapping("/")
    public String list(Model model) {
        model.addAttribute("memberList",members);
        return "list";
    }

web.xml文件中的url-pattern到下面。

<url-pattern>/*</url-pattern>