【问题标题】:Tomcat 7 running Spring ApplicationTomcat 7 运行 Spring 应用程序
【发布时间】:2015-10-31 02:51:19
【问题描述】:

我的 Tomcat 应用程序没有运行(从来没有)。 Tomcat 本身正在运行,但找不到应用程序(见截图):

我有以下项目设置(使用 Spring 4.1 和 Maven):

yahooCalendar-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-4.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd">

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

        <bean id="viewResolver"
            class="org.springframework.web.servlet.view.UrlBasedViewResolver">
            <property name="viewClass"
                value="org.springframework.web.servlet.view.JstlView" />
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
      <display-name>yachtCalendar</display-name>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>

        <servlet>
            <servlet-name>yachtCalendar</servlet-name>
            <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>yachtCalendar</servlet-name>
            <url-pattern>/user.jsp</url-pattern>
            <url-pattern>/user.html</url-pattern>
            <url-pattern>*.html</url-pattern>
        </servlet-mapping>

</web-app>

启动控制台日志时提供以下信息:

编辑: 添加了用户控制器 包 com.afterguard.yachtcalendar.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.afterguard.yachtcalendar.model.User;

@Controller
public class UserController {

    @RequestMapping("/users")
    public ModelAndView getUsers() {
        List<User> users = loadUsers();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("users");
        modelAndView.addObject("users", users);
        return modelAndView;
    }

    public List<User> loadUsers(){
        List<User> list = new ArrayList<User>();
        list.add(new User("User","Name","mail@gmail.com","noPassword"));

        return list;
    }
}

谁能帮我设置一下,告诉我忘记了什么?

编辑:这里附加的 context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--><!-- The contents of this file will be loaded for each web application --><Context>

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->

</Context>

服务器.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--><!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 --><Server port="8005" shutdown="SHUTDOWN">
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/>
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener"/>
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
    <!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the JSSE configuration, when using APR, the
         connector should be using the OpenSSL style configuration
         described in the APR documentation -->
    <!--
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>


    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine defaultHost="localhost" name="Catalina">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
      </Realm>

      <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log." suffix=".txt"/>

      <Context docBase="yachtcalendar" path="/yachtcalendar" reloadable="true" source="org.eclipse.jst.jee.server:yachtcalendar"/></Host>
    </Engine>
  </Service>
</Server>

【问题讨论】:

    标签: java spring maven spring-mvc tomcat


    【解决方案1】:

    您需要一些方法来初始化 Spring 上下文。最简单的方法是将其添加到您的 web.xml:

    <!-- location of spring xml files -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/yachtCalendarApp-servlet.xml</param-value>
    </context-param>
    
    <!-- the listener that kick-starts Spring -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    

    【讨论】:

    • 当您启动 Tomcat 时,您至少应该在控制台日志中看到一些 Spring 消息……是吗?
    • 不,和以前一样的日志。该项目是用maven构建的。这是否意味着什么?
    【解决方案2】:

    原因可能包括以下几点:

    1. 在您的 servlet 中,您仅对 3 种类型的模式使用映射。可能是必须是 *.jsp,因为您的屏幕截图仅包含 jsp 文件

    2. 您的 web.xml 中没有欢迎页面,并且 Tomcat 不知道 jsp 文件

    3. 您的应用被称为“yachtcalendar”(所有字母都很小)但不是yachtCalendar

    你可以试试

    1. http://localhost:8080/yachtcalendar/user 2.http://localhost:8080/yachtcalendar/user.jsp(因为在你的web.xml - user.jsp)
    2. 将您的 url 模式更改为 /* 和 http://localhost:8080/yachtcalendar/user

    你能打印你的用户控制器吗

    【讨论】:

    • 谢谢,但模式应该是正确的。我在 web.xml 中有一个欢迎页面。如上所述。即使尝试更改为 {yachtcalendar} 也没有任何改变。两次尝试确实带来了相同的结果。请参阅上面的用户控制器。但我认为这是一个问题,应用程序不是由 Tomcat 加载的,只有 Tomcat 启动了......
    • 你有 Tomcat 管理控制台中的应用程序吗?可能是您的应用没有部署到服务器?
    • 不,和以前一样的日志。该项目是用maven构建的。这是否意味着什么?
    • 是的。您只构建了您的项目。接下来,您应该将应用程序(战争)部署到服务器。 1. 使用 maven 插件 (tomcat.apache.org/maven-plugin-2.2/tomcat7-maven-plugin/…) 2. 手动 a.您可以将您的应用程序(war 文件)放到 webapps 或通过 tomcat manager localhost:8080/manager
    • 根据我构建这个项目的所有教程,我可以通过Tomcat在服务器上正常运行项目(右键单击项目-运行方式-在服务器上运行-选择Tomcat 7-推送游艇日历在服务器上 - 启动服务器。这就是我之前启动所有项目的方式...
    猜你喜欢
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 2014-04-24
    • 1970-01-01
    • 2020-08-05
    • 2016-06-20
    • 2013-03-26
    相关资源
    最近更新 更多