Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring MVC:RequestMapping不工作,未识别URL路径_Java_Spring_Model View Controller - Fatal编程技术网

Java Spring MVC:RequestMapping不工作,未识别URL路径

Java Spring MVC:RequestMapping不工作,未识别URL路径,java,spring,model-view-controller,Java,Spring,Model View Controller,我在谷歌上搜索过这个问题,但似乎没有人和我有完全相同的问题 我正在尝试设置一个简单的SpringMVC应用程序。以下是相关文件: web.xml <?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="h

我在谷歌上搜索过这个问题,但似乎没有人和我有完全相同的问题

我正在尝试设置一个简单的SpringMVC应用程序。以下是相关文件:

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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" version="2.4">

...

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>MyApp</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>MyApp</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

...
org.springframework.web.context.ContextLoaderListener
MyApp
org.springframework.web.servlet.DispatcherServlet
1.
MyApp
/
myapp-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:mvc="http://www.springframework.org/schema/mvc"
        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">

        <mvc:annotation-driven/>
        <context:component-scan base-package="myapp.gui"/>

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

HomeController.java

@Controller
public class HomeController 
{
    @RequestMapping({"/"})
    public String showHomePage(Map<String,Object> model)
    {
        return "home";
    }
}
@控制器
公共类家庭控制器
{
@请求映射({”/“})
公共字符串显示主页(地图模型)
{
返回“家”;
}
}
我在WEB-INF\views中有一个home.jsp。问题是我的应用程序没有返回主页。我刚得到一个404,尽管Spring正在查找我的控制器(日志上说是这样),但它给出了一个错误:没有识别URL路径

有人能看出我做错了什么吗


Paul

您可以尝试使用/app/*作为servlet映射url,使用/home作为请求映射。然后尝试使用/app/home访问它。映射存在某些问题/*-一旦您使用/app完成其余映射,我们可以考虑删除/app

我曾经想用rest URL完成一个spring MVC项目,但我也找不到任何真正有用的资源。我确实在我的博客上发表了一篇文章,但后来删除了博客。以下是这篇文章的摘录:

我正在使用:

  • NetBeans 6.9.1
  • SpringFramework 3.0.2版本(包括JSTL)
  • JavaJDK6
  • GlassFish服务器3
    • 启动新NetBeans项目
    • 选择Java Web–Web应用程序
    • 单击下一步>
    • 输入项目名称“restMVC”并选择项目位置
    • 单击下一步>
    • 选择GlassFish服务器3–选择Java EE 6 Web
    • 单击下一步>
    • 选中SpringWebMVC–单击配置选项卡,将Dispatcher名称更改为“restMVC”,并将Dispatcher映射更改为“/”
    • 单击“完成”
  • 打开
    restMVC servlet.xml
    –我们希望servlet在特定包的类中搜索
    @Controller
    注释,为此,我们需要在文件中插入一个context:component scan,这意味着我们还需要添加schemeLocation,如下所示:

    
    ...
    
    当然,我们还没有创建包,所以让我们这样做吧

    • 右键单击“源程序包”
    • 去纽约
    • 单击Java包
    • 输入包名
      restMVC.web
  • 现在我们可以创建第一个控制器——

    • 右键单击新的restMVC.web包
    • 去纽约
    • 单击Java类
    • 输入类名
      myController
  • 接下来添加
    @Controller
    注释,然后右键单击–修复导入:

    包restMVC.web;
    导入org.springframework.stereotype.Controller;
    @控制器
    公共类myController{
    }
    
  • 现在我们可以添加一些请求映射URI,记住我们现在有许多不同的选项来使用@RequestMapping注释进行映射,所以一定要查看Spring文档

    包restMVC.web;
    导入java.util.ArrayList;
    导入java.util.List;
    导入org.springframework.stereotype.Controller;
    导入org.springframework.ui.Model;
    导入org.springframework.web.bind.annotation.PathVariable;
    导入org.springframework.web.bind.annotation.RequestMapping;
    @控制器
    公共类myController{
    @请求映射(“/test”)
    公共字符串getTests(模型){
    列表测试=新建ArrayList();
    测试。添加(“”);
    测试。添加(“”);
    model.addAttribute(“测试”,测试);
    返回“测试”;
    }
    @请求映射(“/test/{testId}”)
    公共字符串getTest(@PathVariable字符串testId,Model){
    列表测试=新建ArrayList();
    测试。添加(“测试”+测试ID);
    model.addAttribute(“测试”,测试);
    返回“测试”;
    }
    }
    
  • 我们需要为
    tests.jsp
    创建一个视图。这是通过获取从
    getTests
    getTest
    方法(“测试”)返回的字符串,并使用
    restMVC servlet.xml
    中的viewResolver追加
    .jsp
    ——后面会有更多内容来创建的

    • 右键单击(项目视图)restMVC->Web Pages->Web-INF->jsp文件夹
    • 单击“新建”
    • 单击JSP
    • 输入文件名“tests”
  • 编辑
    tests.jsp
    。在页面顶部输入以下内容:

    
    
    然后在body元素中:

    
    ${test}
    
  • 最后,我们需要确保viewsolver位于
    restMVC servlet.xml
    中。完整的文件应该如下所示:

    
    
  • 您可能需要编辑
    redirect.jsp
    ,使其重定向到
    /test

  • 现在单击“运行主项目”


  • 嗨,gkamal,我也有同样的问题。。因此,在添加应用程序后,它正在工作。。你能告诉我现在如何删除应用程序吗。。?