sách gpt4 ai đã đi

Spring 3 MVC - 调用 Controller 但未找到 View

In lại 作者:行者123 更新时间:2023-12-02 08:17:58 26 4
mua khóa gpt4 Nike

我正在尝试建立一个框架 Spring 3 MVC 项目,但我在渲染 View 时遇到困难。我遵循了 mvc-basic 示例项目和 http://blog.springsource.com/2009/12/21/mvc-simplifications-in-spring-3-0/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+Interface21TeamBlog+%28SpringSource+Team+Blog%29 中描述的结构。设置 web.xml,app-config.xmlmvc-config.xml 文件。 Controller 被调用,但当它到达找到 View 并渲染它的位置时,我收到404错误。文件如下:

web.xml:



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



myServlet
org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

/WEB-INF/app-config.xml


1



myServlet
/app/*



app-config.xml:



xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">














mvc-config.xml:



xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">






















在“Java Resources : src” -> com.myProject -> HelloWorldController.java 中,我有:

package com.myProject;

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

@Controller
@RequestMapping(value="/helloworld")
public class HelloWorldController {

@RequestMapping(method=RequestMethod.GET)
public ModelAndView helloWorld() {
ModelAndView mav = new ModelAndView();
mav.setViewName("helloworld");
mav.addObject("message", "Hello World!");
return mav;
}

@RequestMapping(value="/Second", method = RequestMethod.GET)
public ModelAndView Second(){
ModelAndView mav = new ModelAndView();
mav.setViewName("Second");
mav.addObject("message", "Hello World!");
return mav;
}
}

hiện hữuWebContent/WEB-INF/views中我有:

WebContent (folder)
WEB-INF (folder)
views (folder)
helloworld (folder)
helloworld.jsp (.jsp view)
helloworld.jsp (.jsp view)
welcome.jsp (.jsp view)

View 中有简单的 html。当我请求 http://localhost:8080/projectname/app 时,我正确地获取了 views ->welcome.jsp 页面。但是,当我请求 http://localhost:8080/projectname/app/helloworld hoặc http://localhost:8080/projectname/app/helloworld/ 执行命中正确 Controller 操作,但我得到 HTTP Status 404 -/projectname/WEB-INF/views/helloworld.jsp

谁能告诉我哪里出了问题吗?

Cảm ơn

1 Câu trả lời

有一些问题。第一个是您只在 web.xml 中将/app/* url 分派(dispatch)给 Spring:

/app/*

如果请求映射是/app/helloworld,那很好,但这意味着/helloworld甚至无法到达spring。您可能想要做的是使用 urlrewrite 过滤器将请求映射到/app/* 空间。

获取对 tuckey 的依赖,然后将其添加到您的 web.xml 中:


UrlRewriteFilter
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter


UrlRewriteFilter
/*

然后在 WEB-INF 目录中添加一个名为 urlrewrite.xml 的文件,其中包含:





/images/**
/images/$1


/scripts/**
/scripts/$1


/styles/**
/styles/$1


/**
/app/$1


/app/**
/$1


之后,对/helloworld 的请求应该到达正确的位置。您可能也想更改 Root View Controller :


关于Spring 3 MVC - 调用 Controller 但未找到 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4007356/

26 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com