2013-07-02 40 views
0

嘿,我对Spring和Java Web Development都很陌生。自昨天以来,我一直对此感到震惊。我写了一个控制器来处理我对服务器的请求。但每当我尝试访问页面时,我得到的tomcat 404错误。警告:在名为'spring'的DispatcherServlet中找不到具有URI [/ leaveapp /]的HTTP请求的映射

当我检查Tomcat的CMD提示出现错误

警告:未找到HTTP请求与URI映射[/ leaveapp /]在DispatcherServlet的与名称 '弹簧'

后续是我用过的文件。

为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"; 
    } 
} 
+0

您没有'spring'的请求映射 – DevZer0

+0

什么是您要提供的请求URL? –

+0

我正在请求localhost:8080/leaveapp/index。另外我只是添加了请求映射,但现在它抛出错误,因为beanfactory无法创建bean。 – chandings

回答

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

你的控制器是在包com.imaginea.leaveapp.controller。所以它不会被扫描并用于您的请求。

+0

这就是我发现的。谢谢。 – chandings

1

你看起来牛逼o缺少urlmapping文件或者leaveapp映射。喜欢的东西:

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
    <property name="mappings"> 
    <value> 
     /leaveapp=leaveappController 
    </value> 
    </property> 
</bean> 
+0

嘿,谢谢我刚刚映射的网址,现在我不再收到404错误,但获取Java错误。至少它开始工作 – chandings

+0

嘿我需要一个请求映射为XML在春天。我已经添加了注释! – chandings

相关问题