【问题标题】:Spring mvc controller class is not generated in [project-name]-target-classes directory[project-name]-target-classes 目录中未生成 Spring mvc 控制器类
【发布时间】:2017-05-29 07:49:49
【问题描述】:

我已经创建了一个简单的 spring-mvc maven 项目,但是当我尝试在服务器上运行它并请求任何页面时,它会显示以下错误:


警告:找不到带有 URI 的 HTTP 请求的映射 [/spring-mvc3/outerlink.html] DispatcherServlet 中的名称为“spring”

但后来我发现我的项目没有在 spring-mvc3-target-classes 目录中生成控制器类

这是我的 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>spring-mvc3</display-name>
<welcome-file-list>index.jsp</welcome-file-list>
    <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>/</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:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="first"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"></property>
</bean>

</beans>

这是 EmpController.java 类,存在于 src/main/java 第一个包中:

package first;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class EmpController {

@RequestMapping("/outerlink")
public ModelAndView doWork(HttpServletRequest request)
{
    System.out.println("Entered in Model");
    return new ModelAndView("empform");
}
}

这是我的项目结构,我的项目名称是 spring-mvc3

请帮我解决这个问题,提前谢谢

【问题讨论】:

  • 试试:/spring-mvc3/outerlink
  • 这是一个安静的服务还是一个 html 调用?因为我发现它是一个网络服务
  • thasnks jens 和 rahul,我试过 /spring-mvc3/outerlink 但仍然遇到同样的错误

标签: java spring jsp spring-mvc servlets


【解决方案1】:

我认为您的主要问题是您使用的是 html 页面,如果您使用的是 .html 页面,则 ModelAndView 无法执行。为此,您需要将该页面设置为 .jsp 文件。

如果您想按原样使用 html 文件,则需要重定向它。由于 html 页面是静态页面,您需要在 spring-servlet.xml 中配置如下

<mvc:resources mapping="/resources/**" location="/resources/" />

要改正:return "redirect:resources/htmlPageName"

Spring xml 文件命名空间应该有 spring mvc。

    <?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:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc" 
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            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
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

        <mvc:annotation-driven />
        <context:component-scan base-package="first" />

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

    </beans>

放在jars下面,java版本应该是java 8

【讨论】:

  • 谢谢..但我尝试使用 jsp 页面而不是它也显示相同的错误
  • 你能发布你的项目结构吗
  • 你试过把它作为调试运行吗?如果没有,请在控制器上放置一个调试点并检查您的请求是否到达控制器,然后请更新我
  • 我在 spring xml 文件中检查了你的命名空间,它看起来就像你没有在你的命名空间中使用 spring mvc。请检查我的更新答案
  • 我已经用你的替换了我的 spring-servlet 文件的代码,但是当我运行它时,它给了我以下错误:org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 1 in XML document from ServletContext 资源 [/WEB-INF/spring-servlet.xml] 无效;嵌套异常是 org.xml.sax.SAXParseException;行号:1;列号:8;不允许匹配“[xX][mM][lL]”的处理指令目标。
猜你喜欢
  • 2014-02-15
  • 1970-01-01
  • 2013-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多