tomcat8部署无法使用所请求的资源

时间:2017-02-20 14:12:03

标签: java eclipse spring maven tomcat8

每次我收到msg“请求的资源不可用”。我在spring框架中创建了一个maven项目。它的构建成功,但当我想用tomcate7或tomcate8执行时,它会给出同样的错误。 java版本是1.8。 它愚蠢的示例代码,我不知道为什么我会收到这个错误。

这里是代码段:

1.POM XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <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>AquaEPP</groupId>
        <artifactId>AquaEPP</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>

        <name>AquaEPP</name>
        <url>http://maven.apache.org</url>

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

        <dependencies>
            <!-- Spring core & mvc -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>${spring.version}</version>
                <type>jar</type>
                <scope>compile</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
                <type>jar</type>
                <scope>test</scope>
            </dependency>

            <!-- CGLib for @Configuration -->
            <dependency>
                <groupId>cglib</groupId>
                <artifactId>cglib-nodep</artifactId>
                <version>${cglib.version}</version>
                <scope>runtime</scope>
            </dependency>


            <!-- Servlet Spec -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>2.4</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>jsp-api</artifactId>
                <version>2.1</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
                <scope>provided</scope>
            </dependency>
             <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>1.7.0</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.0</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>1.7.0</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.14</version>
            </dependency>
        </dependencies>

        <repositories>
            <repository>
                <id>springsource-milestones</id>
                <name>SpringSource Milestones Proxy</name>
                <url>https://oss.sonatype.org/content/repositories/springsource-milestones</url>
            </repository>
        </repositories>

        <build>
            <finalName>AquaEPP</finalName>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.0.2</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                    </configuration>
                </plugin>
                <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.19.1</version>
                        <configuration>
                            <testFailureIgnore>true</testFailureIgnore>
                        </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
  1. WEB.XML

        <?xml version="1.0" encoding="UTF-8"?>
        <web-app 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>AquaEPP</display-name>
            <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
            <welcome-file>default.html</welcome-file>
            <welcome-file>default.htm</welcome-file>
            <welcome-file>default.jsp</welcome-file>
          </welcome-file-list>
            <context-param>
                <param-name>contextClass</param-name>
                <param-value>
                    org.springframework.web.context.support.AnnotationConfigWebApplicationContext
                </param-value>
            </context-param>
            <listener>
                <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
            </listener>
    
            <servlet>
                <servlet-name>SpringDispatcher</servlet-name>
                <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                <init-param>
                    <param-name>contextClass</param-name>
                    <param-value>
                        org.springframework.web.context.support.AnnotationConfigWebApplicationContext
                    </param-value>
                </init-param>
                <init-param>
                    <param-name>contextConfigLocation</param-name>
                    <param-value>com.exp.aqua</param-value>
                </init-param>
                <load-on-startup>1</load-on-startup>
            </servlet>
            <servlet-mapping>
                <servlet-name>SpringDispatcher</servlet-name>
                <url-pattern>/</url-pattern>
            </servlet-mapping>
    
            <session-config>
                <session-timeout>30</session-timeout>
            </session-config>
        </web-app>
    
  2. 3.controller class

            package com.exp.aqua.controller;
    
            import java.io.IOException;
    
            import javax.servlet.http.HttpServletResponse;
    
            import org.springframework.stereotype.Controller;
            import org.springframework.web.bind.annotation.RequestMapping;
            import org.springframework.web.servlet.ModelAndView;
    
            @Controller
            public class HomeController {
    
                @RequestMapping(value="/home")
                public ModelAndView test(HttpServletResponse response) throws IOException{
                    return new ModelAndView("home");
                }
            }
    

    4.MvcConfiguration类

                package com.exp.aqua.config;
    
                import org.springframework.context.annotation.Bean;
                import org.springframework.context.annotation.ComponentScan;
                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.config.annotation.ResourceHandlerRegistry;
                import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
                import org.springframework.web.servlet.view.InternalResourceViewResolver;
    
                @Configuration
                @ComponentScan(basePackages="com.exp.aqua")
                @EnableWebMvc
                public class MvcConfiguration extends WebMvcConfigurerAdapter{
    
                    @Bean
                    public ViewResolver getViewResolver(){
                        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
                        resolver.setPrefix("/WEB-INF/views/");
                        resolver.setSuffix(".jsp");
                        return resolver;
                    }
    
                    @Override
                    public void addResourceHandlers(ResourceHandlerRegistry registry) {
                        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
                    }
    
    
                }
    

    5.index.jsp

                <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
                    pageEncoding="ISO-8859-1"%>
                <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
                <html>
                <head>
                <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
                <title>Spring Demo</title>
                </head>
                <body>
                <br>
                    <div style="text-align:center">
                        <h2>
                            Hey You..!! This is your 1st Spring MCV Tutorial..<br> <br>
                        </h2>
                        <h3>
                            <a href="home.jsp">Click here to See Welcome Message... </a>(to
                            check Spring MVC Controller... @RequestMapping("/welcome"))
                        </h3>
                    </div>
                </body>
                </html>
    
    1. 针对home.jsp

              <%@page contentType="text/html" pageEncoding="UTF-8"%>
              <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                 "http://www.w3.org/TR/html4/loose.dtd">
      
              <html>
                  <head>
                      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                      <title>Home</title>
                  </head>
                  <body>
                      <h1>Hello World!</h1>
                      <p>This is the homepage!</p>
                  </body>
              </html>
      

1 个答案:

答案 0 :(得分:0)

我认为你的tomcat没问题,对我有用:

1.-右键单击您的项目 - &gt;属性 - &gt;部署组件 - &gt;添加 - &gt; Java构建路径条目 - &gt; Maven依赖

2.-在MvcConfiguration @ComponentScancom.exp.aqua.controller的基本广告必须为index.jsp

3.-在.jsp移除href属性中的<a href="home">扩展名:index.jsp

最后,请记住您的WebContent必须位于home.jsp文件夹和views中的 setContentView(R.layout.activity_main); ListView listView = (ListView) findViewById(R.id.listView); BackgroundWorker backgroundWorker = new BackgroundWorker(this, listView); backgroundWorker.execute("argument");

enter image description here