用spring mvc模式写了一段代码,但一直都会提示404,不知道错误在哪,求指点。

用springmvc 和spring jdbc谢了一段代码,但一直提示404错误,不知道该如何解决,已经困扰很多天了。(Dao中只写了增加,没有写service,只是想试一下能不能连接到数据库,customer只有id和name)
web.xml文件内容如下:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  </listener>

<servlet> 
    <servlet-name>CustomerMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:*beans.xml</param-value>
    </init-param> 
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>CustomerMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


      <filter>
    <filter-name>setcharacter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>setcharacter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

CustomerMVC-servlet文件内容如下:

 <?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:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="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  
        http://www.springframework.org/schema/mvc  
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="controller"/>
    <mvc:annotation-driven/>
    <bean 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> 

</beans>

Controller文件:

 package Controller;

import model.Customer;

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

import dao.CustomerDao;

@Controller
@RequestMapping("/jsp")
public class CustomerController {
    private CustomerDao dao;

    @RequestMapping(value="/add",method=RequestMethod.GET)
    public String add(Model model){
        model.addAttribute(new Customer());
        return "jsp/add";
    }

    @RequestMapping(value="/add",method=RequestMethod.POST)
    public String add(Customer customer){
        dao.add(customer);
        return "redirect:/jsp/add";
    }

}

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

    <context:annotation-config/>
    <context:component-scan base-package = "Controller"/>
    <context:component-scan base-package = "dao"/>
    <context:component-scan base-package = "model"/>


<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="{jdbc.password}"/>
</bean>
<context:property-placeholder location="classpath:jdbc.properties"/>
</beans>

jdbc.properties文件:

 jdbc.driverClassName = oracle.jdbc.driver.OracleDriver
jdbc.url = jdbc:oracle:thin:@localhost:1521:WAREHOUSE
jdbc.username = sys as SYSDBA
jdbc.password = oracle123

工程结构:图片说明
错误提示:
图片说明
图片说明

我已经把CustomerMVC-servlet的controller改成了Controller,但是还是会出现404,但是不会抛出异常。会出现最后一张图的提示。

3个回答

Web.xml 里面的 CustomerMVC 改成springmvc 试一下

sinat_30222349
sinat_30222349 我改了,还是同样的错误。
4 年多之前 回复
     <context:component-scan base-package="controller"/>  
     你的这个报名controller第一个字母是大写的,应该是下边的
     <context:component-scan base-package="Controller"/>  
sinat_30222349
sinat_30222349 对的,我改过了,不抛出异常了,但是还是会出现我问题里的最后一张图的提示信息:No mapping found for HTTP request with URI 。。。。。现在不知道怎么改了,不知道是什么地方还有问题。
4 年多之前 回复

试试把springServlet的url映射改成如下:

 <servlet-mapping>
        <servlet-name>CustomerMVC</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐