Java 在名为';appServlet';

Java 在名为';appServlet';,java,spring,spring-mvc,sts-springsourcetoolsuite,tcserver,Java,Spring,Spring Mvc,Sts Springsourcetoolsuite,Tcserver,我绝对是Java和Spring的新手,我想从示例中学习 我正在使用开箱即用的配置/安装 MacOSX Springsource工具套件作为IDE 弹簧2.8.1.释放 vfabric-tc-server-developer-2.6.1.1版本 我试图基于“Spring模板项目”生成一个新项目。然后我选择了“SpringMVC项目”。将生成示例项目。之后,在不修改任何内容的情况下,我尝试通过“运行方式”执行de“home.jsp”页面。Web服务器启动,最后我在控制台选项卡中收到了错误 在中找

我绝对是Java和Spring的新手,我想从示例中学习

我正在使用开箱即用的配置/安装

  • MacOSX
  • Springsource工具套件作为IDE
  • 弹簧2.8.1.释放
  • vfabric-tc-server-developer-2.6.1.1版本
我试图基于“Spring模板项目”生成一个新项目。然后我选择了“SpringMVC项目”。将生成示例项目。之后,在不修改任何内容的情况下,我尝试通过“运行方式”执行de“home.jsp”页面。Web服务器启动,最后我在控制台选项卡中收到了错误

在中找不到URI为[/myproject/]的HTTP请求的映射 名为“appServlet”的DispatcherServlet

以及这些网页中的其他输出:

  • http://localhost:8080/myproject/WEB-INF/views/home.jsp
  • http://localhost:8080/myproject

在这里,您可以看到有关我的项目的结构(为STS自动生成)的图像:

怎么了

在这里您可以看到web.xml文件的内容:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

</web-app>

上下文配置位置
/WEB-INF/spring/root-context.xml
org.springframework.web.context.ContextLoaderListener
appServlet
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/spring/appServlet/servlet-context.xml
1.
appServlet
/
root context.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->

</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

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

</beans:beans>

最后,servlet context.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->

</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

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

</beans:beans>


有人有办法解决这个问题吗?

WEB-INF下的所有内容都无法从外部访问,MVC框架的要点是在分派到视图之前调用控制器,因此无论如何都不应该直接调用JSP


您可能没有任何Spring控制器映射到根URL,因此很明显,URL
http://localhost:8080/myproject/

Spring的惯例是假定
DispatcherServlet
的web.xml中的
与Spring servlet上下文xml文件的开头匹配。将
servlet context.xml
重命名为
appServlet context.xml
,看看这是否有帮助。

向应用程序添加一个控制器,该控制器返回一个名为“home”的ModelAndView实例。然后配置一些到该控制器的处理程序映射,并尝试使用类似于以下URL的URL访问您的web应用:
http://localhost:8080/myproject/home.do
。可以找到更多信息,并且。

尝试发现相同的错误。事实上,在web.xml文件中,有一行指定servlet的名称。。。现在我已经更改为您建议的版本(加上文件的重命名)。。。但结果是一样的。contextConfigLocation/WEB-INF/spring/appServlet/appServlet上下文。xml@AlfonsoDominguez@duffymo自动生成的项目有一个名为“HomeController.java”的文件,其内容如下:`package com.mycompany.myapp;[…]省略的部分[…]/***处理对应用程序主页的请求@控制器公共类HomeController{/**通过返回其名称来选择要渲染的主视图。*/@RequestMapping(value=“/”,method=RequestMethod.GET)公共字符串home(Locale,Model Model){String str=“Server message”;Model.addAttribute(“serverTime”,str);返回“home”}“你是这么说的吗?是的,我就是这个意思,你试着访问URL
http://localhost:8080/myproject/home
?该URL似乎是配置文件中的正确URL。