0

我是 Spring MVC 的新手。我的项目结构如下:

在此处输入图像描述

运行 Web 项目时出现以下错误:

HTTP Status 404 - 

type Status report

message 

description The requested resource is not available.


Apache Tomcat/7.0.65

AppConfig.java:

package config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
public class AppConfig {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }
}

AppInitializer.java:

package config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { AppConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }
}

用户控制器.java:

package controller;

import model.User;

import org.springframework.beans.factory.annotation.Autowired;
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 service.UserService;
@Controller
public class UserController {
    @Autowired
    private UserService  userService;

    @RequestMapping(value="/users",method= RequestMethod.GET)
    public String listUsers(Model model) {
        model.addAttribute("user",new User());
        model.addAttribute("listUser" , userService.listUser());

        return "user";
    }
}

启动日志:

Jul 13, 2016 12:14:24 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:demo' did not find a matching property.
Jul 13, 2016 12:14:24 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:test' did not find a matching property.
Jul 13, 2016 12:14:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/7.0.65
Jul 13, 2016 12:14:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Oct 9 2015 08:36:58 UTC
Jul 13, 2016 12:14:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         7.0.65.0
Jul 13, 2016 12:14:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 7
Jul 13, 2016 12:14:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            6.1
Jul 13, 2016 12:14:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Jul 13, 2016 12:14:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jre1.8.0_72
Jul 13, 2016 12:14:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_72-b15
Jul 13, 2016 12:14:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Jul 13, 2016 12:14:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         D:\bruno\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Jul 13, 2016 12:14:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         D:\apache-tomcat-7.0.65\apache-tomcat-7.0.65
Jul 13, 2016 12:14:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=D:\bruno\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Jul 13, 2016 12:14:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=D:\apache-tomcat-7.0.65\apache-tomcat-7.0.65
Jul 13, 2016 12:14:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=D:\bruno\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Jul 13, 2016 12:14:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=D:\apache-tomcat-7.0.65\apache-tomcat-7.0.65\endorsed
Jul 13, 2016 12:14:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Jul 13, 2016 12:14:24 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre1.8.0_72\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;C:/Program Files/Java/jre1.8.0_72/bin/server;C:/Program Files/Java/jre1.8.0_72/bin;C:/Program Files/Java/jre1.8.0_72/lib/amd64;C:\oraclexe\app\oracle\product\11.2.0\server\bin;;C:\ProgramData\Oracle\Java\javapath;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\Program Files (x86)\Microsoft Application Virtualization Client;C:\Program Files\TortoiseSVN\bin;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\;C:\ProgramData\Oracle\Java\javapath;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\Program Files (x86)\Microsoft Application Virtualization Client;C:\Program Files\TortoiseSVN\bin;C:\windows\System32\WindowsPowerShell\v1.0\;;D:\apache-ant-1.9.7-bin\apache-ant-1.9.7\bin;C:\Program Files\Java\jdk1.8.0_77\bin;C:\Users\kushagra_maurya\Desktop\ABC\eclipse-jee-juno-SR2-win32-x86_64\eclipse;;.
Jul 13, 2016 12:14:24 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8084"]
Jul 13, 2016 12:14:24 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8085"]
Jul 13, 2016 12:14:24 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1261 ms
Jul 13, 2016 12:14:24 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jul 13, 2016 12:14:24 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.65
Jul 13, 2016 12:14:25 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [134] milliseconds.
Jul 13, 2016 12:14:26 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8084"]
Jul 13, 2016 12:14:26 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8085"]
Jul 13, 2016 12:14:26 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1664 ms

我在这里想念什么?任何帮助将不胜感激。

4

1 回答 1

0

我很确定您正在调用服务器的根路径(例如localhost:8080/),对吗?

Eclipse 使用您的项目名称在上下文路径中部署 WebApplication。工作路径应该是localhost:8080/<yourProjectName>/

您可以通过打开项目设置 -> Web 项目设置 -> 上下文根来更改此行为。

编辑:因为你唯一的端点是/users你必须打电话localhost:8080/<yourProjectName>/users

于 2016-07-12T13:56:34.823 回答