无法完全基于注解运行Spring MVC

ej83mcc0  于 12个月前  发布在  Spring
关注(0)|答案(1)|浏览(204)

我是Spring MVC的新手。我的项目结构如下:
x1c 0d1x的数据
我在运行Web项目时遇到以下错误:
HTTP状态404 -
类型状态报告
消息
请求的资源不可用。
问题Apache Tomcat/6.0.39
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[] { "/" };
    }
}


UserController.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


我错过了什么?

pftdvrlh

pftdvrlh1#

我很确定你正在调用服务器的根路径(例如localhost:8080/),对吗?
Eclipse将WebApplication部署在带有项目名称的上下文路径中。
您可以通过打开项目设置-> Web项目设置->上下文根来更改此行为。
编辑:由于您唯一的端点是/users,因此必须调用localhost:8080/<yourProjectName>/users

相关问题