Spring JDBC MVC项目给出了404

时间:2017-03-07 18:46:02

标签: java spring maven spring-mvc

我已经和Spring框架打了几个星期了,我得到了 要显示的普通MVC页面,我将JDBC打印到控制台上。 但我似乎无法让这些一起工作......

此时我已经完成了无数的教程和每一个教程 似乎有某种类型的错误或问题。

我想我终于成功地聚集了一个非常体面和有效的构建,但是没有,仍然只得到404.

我认为问题可能出在spring-servlet.xml文件中:

<context:component-scan base-package="src" />

我不确定你是否可以加载这样的项目,但这是我能想到的唯一一件事。

我也包括所有其他文件。

控制台错误

WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:spring-mvc' did not find a matching property.

SEVERE: Servlet [spring] in web application [/spring-mvc] threw load() exception
java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1275)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1109)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:520)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:501)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1050)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:989)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4903)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5213)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>spring-mvc</groupId>
  <artifactId>spring-mvc</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <properties>
    <java.version>1.8</java.version>
    <spring.version>3.1.0.RELEASE</spring.version>
    <cglib.version>2.2.2</cglib.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc-portlet</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
    </dependency>
    <dependency>
      <groupId>org.gatein.common</groupId>
      <artifactId>common-logging</artifactId>
      <version>2.2.2.Final</version>
    </dependency>
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>2.8.1</version>
      <type>pom</type>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>6.0.5</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
  </dependencies>
</project>

弹簧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:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 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">

 <!-- Telling container to take care of annotations stuff -->
 <context:annotation-config />

 <!-- Declaring base package -->
 <context:component-scan base-package="controller" />

 <!-- Adding view resolver to show jsp's on browser -->
 <bean id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
 </bean>

 <!-- Declare beans -->
 <bean id="userDao" class="dao.UserDaoImpl" />
 <bean id="userService" class="services.UserServiceImpl" />

 <!-- Declare datasource bean -->
 <bean id="dataSource"
  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url" value="jdbc:mysql://localhost:3306/userdb" />
  <property name="username" value="root" />
  <property name="password" value="38613861" />
 </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>SpringJDBCTemplate</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>

homePageController

package controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import domain.*;
import services.*;

@Controller
public class HomePageController {

 @Autowired
 UserService userService;

 @RequestMapping("/register")
 public ModelAndView registerUser(@ModelAttribute User user) {

  List<String> genderList = new ArrayList<String>();
  genderList.add("male");
  genderList.add("female");

  List<String> cityList = new ArrayList<String>();
  cityList.add("delhi");
  cityList.add("gurgaon");
  cityList.add("meerut");
  cityList.add("noida");

  Map<String, List> map = new HashMap<String, List>();
  map.put("genderList", genderList);
  map.put("cityList", cityList);
  return new ModelAndView("register", "map", map);
 }

 @RequestMapping("/insert")
 public String inserData(@ModelAttribute User user) {
  if (user != null)
   userService.insertData(user);
  return "redirect:/getList";
 }

 @RequestMapping("/getList")
 public ModelAndView getUserLIst() {
  List<User> userList = userService.getUserList();
  return new ModelAndView("userList", "userList", userList);
 }

 @RequestMapping("/edit")
 public ModelAndView editUser(@RequestParam String id,
   @ModelAttribute User user) {

  user = userService.getUser(id);

  List<String> genderList = new ArrayList<String>();
  genderList.add("male");
  genderList.add("female");

  List<String> cityList = new ArrayList<String>();
  cityList.add("delhi");
  cityList.add("gurgaon");
  cityList.add("meerut");
  cityList.add("noida");

  Map<String, Object> map = new HashMap<String, Object>();
  map.put("genderList", genderList);
  map.put("cityList", cityList);
  map.put("user", user);

  return new ModelAndView("edit", "map", map);
 }

 @RequestMapping("/update")
 public String updateUser(@ModelAttribute User user) {
  userService.updateData(user);
  return "redirect:/getList";
 }

 @RequestMapping("/delete")
 public String deleteUser(@RequestParam String id) {
  System.out.println("id = " + id);
  userService.deleteData(id);
  return "redirect:/getList";
 }
}

项目树

Project tree from Eclipse Neon2

1 个答案:

答案 0 :(得分:0)

不是我的答案,而是解决案件的答案,作为答案发布,因为它深埋在评论中。

  

抱歉延迟......它仍然不对。我知道因为   Eclipse不应该将包显示为main.java.controller .... Maven   忽略该文件夹,然后从java文件夹下开始   src文件夹本身。你的项目肯定有问题。尝试   清理一切,从eclipse中删除项目,删除   (物理上)文件夹.settings和文件.classpath和文件   。项目。在控制台上继续你的文件夹项目并运行mvn clean   eclipse:clean然后打开if的所有文件夹(就像我展示的图像一样)   你把它贴在这里,这样我就可以看出它是错的了

- Jorge Campos