首页 > 解决方案 > 请求的资源在 spring mvc 中的 Tomcat v7 中不可用

问题描述

我正在尝试在 Tomcat v7 上运行一个简单的 spring mvc 应用程序。尝试运行以下网址时:

http://localhost:8080/springmvc/

我收到错误消息:

The requested resource is not available

我可以使用以下网址打开 tomcat 主页:

http://localhost:8080

我已将eclipse中tomcat服务器选项的服务器位置更改为:

Use Tomcat installation

还是同样的问题。下面是我的web.xml

<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>/springmvc/*</url-pattern>
    </servlet-mapping>

我还将 url 模式更新为//*

还是同样的问题。有人可以帮忙吗

编辑:

控制器

@Controller
@RequestMapping("/employee-module")
public class EmployeeController {

    @Autowired
    EmployeeService service;

    @RequestMapping(value="/getAllEmployees", method=RequestMethod.GET)
    public String getAllEmployees(Model model){

        List<Employee> l = service.getAllEmployees();
        model.addAttribute("employees", l);
        return "employeeinfodisplay";
    }


}

编辑:

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">

    <display-name>Spring Web MVC Hello World Application</display-name>

    <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>/springmvc</url-pattern>
    </servlet-mapping>

</web-app>

下面是包

在此处输入图像描述

Spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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"> 

    <context:component-scan base-package="com.employee" />

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

Tomcat 服务器启动日志:

Mar 03, 2019 3:49:43 PM org.apache.catalina.core.AprLifecycleListener init
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\jdk1.7.0_80\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;C:\app\SAURABH\product\11.2.0\dbhome_1\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\iCLS\;C:\Program Files\Intel\Intel(R) Management Engine Components\iCLS\;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\windows\System32\OpenSSH\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Git\cmd;C:\Program Files\Notepad++;C:\Program Files\Java\jdk1.8.0_191\bin;"C:\Users\SAURABH\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Notepad++";C:\Program Files\Java\jdk1.8.0_191\bin;;.
Mar 03, 2019 3:49:44 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:springmvc' did not find a matching property.
Mar 03, 2019 3:49:44 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Mar 03, 2019 3:49:44 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Mar 03, 2019 3:49:44 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 827 ms
Mar 03, 2019 3:49:44 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 03, 2019 3:49:44 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
Mar 03, 2019 3:49:48 PM org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [265] milliseconds.
Mar 03, 2019 3:49:48 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Tools\Apachetomcat_v7\webapps\docs
Mar 03, 2019 3:49:51 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Tools\Apachetomcat_v7\webapps\examples
Mar 03, 2019 3:49:54 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
Mar 03, 2019 3:49:54 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
Mar 03, 2019 3:49:54 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache@4ed39f12')
Mar 03, 2019 3:49:54 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Tools\Apachetomcat_v7\webapps\host-manager
Mar 03, 2019 3:49:56 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Tools\Apachetomcat_v7\webapps\manager
Mar 03, 2019 3:49:58 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Tools\Apachetomcat_v7\webapps\ROOT
Mar 03, 2019 3:50:00 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Mar 03, 2019 3:50:00 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Mar 03, 2019 3:50:00 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 15892 ms

服务器配置:

在此处输入图像描述

编辑 :

尽管尝试了很多次,我还是不得不从网上下载实际示例并在 Eclipse 上运行它。但现在我得到另一个错误:

Mar 03, 2019 8:19:37 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/springmvcexample/employee-module] in DispatcherServlet with name 'spring'

以下是相关文件:

控制器

@Controller
@RequestMapping("employee-module")
public class EmployeeController 
{
    @Autowired
    EmployeeManager manager;

    @RequestMapping(value = "/getAllEmployees", method = RequestMethod.GET)
    public String getAllEmployees(Model model)
    {
        model.addAttribute("employees", manager.getAllEmployees());
        return "employeesListDisplay";
    }

Web.xml

<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">

    <display-name>Spring Web MVC Hello World Application</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>


    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

spring-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    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/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.howtodoinjava.demo.*" />
    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>


</beans>

标签: javaspringeclipsespring-mvcservlets

解决方案


推荐阅读