1

我尽我所能查看了这里的其他答案,但似乎没有什么完全适合我的问题/解决我的问题。本质上,当我尝试从我的索引访问ownedGames 时,我得到了上述错误。

我觉得我已经正确完成了映射,但显然我没有。所以在这里我会发布任何看起来可能相关的文件。

index.html(我点击给出错误的链接)

<html>
<head>
    <title>xbox voting</title>
</head>
<body>
    <a href="wantedGames">Vote on Games!</a> <br />
    <a href="ownedGames">View the Games We Own!</a>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>Spring3-Hibernate</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

spring-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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <context:annotation-config />
   <context:component-scan base-package="net.nerdery.xboxvoting.controller" />

    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>


    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="jdbc.properties" />

    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
        p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
        p:password="${jdbc.password}" />


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven />
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
</beans>

游戏控制器

package net.nerdery.xboxvoting.controller;

import java.util.Map;


import net.nerdery.xboxvoting.domain.Game;
import net.nerdery.xboxvoting.service.GameService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class GameController {

    @Autowired
    private GameService gameService;

    @RequestMapping("/XboxVoting/ownedGames")
    public String listOwnedGames(Map<String, Object> map) {
        map.put("game", new Game());
        map.put("ownedGamesList", gameService.listOwnedGames());

        return "game";
    }

    @RequestMapping("/wantedGames")
    public String listWantedGames(Map<String, Object> map) {
        map.put("game", new Game());
        map.put("wantedGamesList", gameService.listOwnedGames());

        return "game";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addGame(@ModelAttribute("game")
    Game game, BindingResult result) {

        gameService.addGame(game);

        return "redirect:/ownedGames";
    }

}

我确定这是我在某处遗漏的一些愚蠢的语法错误,但我是 Spring 新手(今天才开始),几乎不知道我在做什么。所以感谢您的帮助!

4

2 回答 2

1

你需要 <url-pattern>/</url-pattern> 改变 <url-pattern>/*</url-pattern>

于 2013-07-26T13:24:57.210 回答
0

您在 href 中使用绝对路径,因此您应该在那里指定完整路径,
或者使用相对路径(在 href 的开头附加 /)代替

 <a href="/wantedGames">Vote on Games!</a> <br />
 <a href="/XboxVoting/ownedGames">View the Games We Own!</a>

也尝试添加method = RequestMethod.GET到您的控制器。

我不确定您的 URL 结构。

于 2012-07-11T06:40:35.260 回答