【问题标题】:No mapping found for HTTP request with URI [/learning/avionSave] in DispatcherServlet with name 'appServlet'在名称为“appServlet”的 DispatcherServlet 中找不到具有 URI [/learning/avionSave] 的 HTTP 请求的映射
【发布时间】:2017-06-28 12:14:19
【问题描述】:

我仍然收到这个问题: 警告:org.springframework.web.servlet.PageNotFound - 在 DispatcherServlet 中找不到带有 URI [/learning/avionSave] 的 HTTP 请求的映射,名称为“appServlet” 在我的 avion.jsp 页面中按下 AvionNew 按钮后。

该项目的工作方式如下:它将加载主页,我将通过点击 avion 按钮重定向到 avion.jsp 页面。将出现一个名为 AvionNew 的新按钮。当我按下这个按钮时,我希望它加载 @RequestMapping(value = "/avionSave" .. 并打印到控制台只是 System.out.println("are we here?"); 但它不起作用,它仍然向我显示该错误:(

这是我的 home.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
    <title>Home</title>
</head>
<body>
<h1>
    Hello world!  
</h1>

<P>  The time on the server is ${serverTime}. </P>

<a href="http://localhost:8080/learning/">Home</a>
<a href="http://localhost:8080/learning/avion">Avion</a>
</body>
</html>

这是我的 avion.jsp:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
    <title>Avion</title>
</head>
<body>
<h1>
    Nazdar  
</h1>

${hodnota}


<a href="http://localhost:8080/learning/">Home</a>
<a href="http://localhost:8080/learning/avion">Avion</a>
<br>
<a href="http://localhost:8080/learning/avionSave" name=odkaz>AvionNew</a>

</body>
</html>

这是我的 HomeController.java:

@Controller
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    /**
     * Simply selects the home view to render by returning its name.
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Welcome home! The client locale is {}.", locale);

        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

        String formattedDate = dateFormat.format(date);

        model.addAttribute("serverTime", formattedDate );

        return "home";
    }

    @RequestMapping(value = "/avion", method = RequestMethod.GET)
    public String avion(Locale locale, Model model) {
        model.addAttribute("hodnota", "here");
        System.out.println("still here...");
        return "avion";
    }

    @RequestMapping(value = "/avionSave", method = RequestMethod.GET,params="odkaz")
    public String avionOdkaz(Locale locale, Model model) {
        System.out.println("are we here?");
        return "avion";
    }
}

这是我的 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>

这是我的 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.xsd">

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

</beans>

这是我的 servlet-context.xml:

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

    <!-- 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.dodo.learning" />



</beans:beans>

我在谷歌上搜索了很多,看到很多类似的主题,但我无法修复它:( 有人可以帮我吗? 谢谢

【问题讨论】:

  • 共享 servlet-context.xml
  • 您是在根上下文或/learning 下将应用程序部署到您的tomcat 吗?请添加您的root-context.xmlservlet-context.xml 文件
  • 我在主题中添加了根上下文和 servlet 上下文。如果我正在部署它?嗯,我正在使用 tomcat 在我的本地主机上运行它

标签: java spring-mvc controller


【解决方案1】:

您指向错误的上下文路径。使用 Tomcat,您可以在启动时在日志中看到类似这样的内容

OK - Deployed application at context path /foo

因此您可以了解应用程序的上下文。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多