0

嘿,我对 Spring 以及 Java Web 开发都很陌生。从昨天开始,我一直在努力解决这个问题。我编写了一个控制器来处理我对服务器的请求。但是每当我尝试访问该页面时,我都会收到 tomcat 404 错误。

当我检查 tomcat cmd 提示时,我得到了错误

警告:在名称为“spring”的 DispatcherServlet 中找不到具有 URI [/leaveapp/] 的 HTTP 请求的映射

以下是我使用过的文件。

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="com.imaginea.leaveapp.model" />

    <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="/WEB-INF/jdbc.properties" />

    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
        p:driverClassName="com.mysql.jdbc.Driver"
        p:url="jdbc:mysql://localhost:3306/organization" p:username="root"
        p:password="root" />


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath: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>

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>Leave Application</display-name>
    <welcome-file-list>
        <welcome-file>index.html</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>

控制器类

package com.imaginea.leaveapp.controller;

import java.util.Map;

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;

import com.imaginea.leaveapp.model.LeaveApplication;
import com.imaginea.leaveapp.services.LeaveApplicationService;

@Controller
public class LeaveApplicationController {
    @Autowired
    private LeaveApplicationService leaveApplicationService;

    @RequestMapping("/index")
    public String leaveList(Map<String, Object> map) {

        map.put("leave", new LeaveApplication());
        map.put("leaveList", leaveApplicationService.getLeaveDetails());

        return "leaveapp";
    }

    @RequestMapping(value = "/apply", method = RequestMethod.POST)
    public String applyLeave(@ModelAttribute("leave") LeaveApplication leave, BindingResult result){
        leaveApplicationService.addLeave(leave);
        return "redirect:/index";
    }

    @RequestMapping("/approve/{leaveID}")
    public String approve(@PathVariable("leaveID") Integer leaveID){
        LeaveApplication leave = leaveApplicationService.getLeaveDetail(leaveID);
        leave.setStatus("approved");
        leaveApplicationService.updateLeave(leave);
        return "redirect:/index";
    }

    @RequestMapping("/reject/{leaveID}")
    public String reject(@PathVariable("leaveID") Integer leaveID, BindingResult result){
        LeaveApplication leave = leaveApplicationService.getLeaveDetail(leaveID);
        leave.setStatus("rejected");
        leaveApplicationService.updateLeave(leave);
        return "redirect:/index";
    }
}
4

2 回答 2

3
<context:component-scan base-package="com.imaginea.leaveapp.model" />

您的控制器在包中com.imaginea.leaveapp.controller。因此,它不会被扫描并用于您的请求。

于 2013-07-02T11:00:41.773 回答
1

您似乎缺少 urlmapping 文件或 leaveapp 的映射。就像是 :

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
   <property name="mappings">
    <value>
       /leaveapp=leaveappController
    </value>
   </property>
</bean>
于 2013-07-02T05:08:15.843 回答