gpt4 book ai didi

java - 无法使用 jsp 文件名作为 RequestMapping 值解析 WEB-INF/文件夹内的 View

转载 作者:行者123 更新时间:2023-12-01 12:35:05 26 4
gpt4 key购买 nike

好吧,这对于 spring 初学者来说一直是一个问题,可能有很多类似的问题,但每次我输入这个问题时,我总是最终阅读我无法理解的间接解决方案,就像标题所说的那样,如果我使用文件名映射页面,我无法解析 WEB-INF/views 文件夹中的 View (jsp)。

更新:我一直在广泛搜索 noHandlerFound/NoMapping Found for a URI 问题,我刚刚了解到 Controller 类名称将通过删除 ControllerClassNameHandlerMapping 来解析controller 并将 non-controller 名称小写,它将用作 url,

但我仍然无法在启动时加载我非常简单的页面,无论我在哪里搜索,总是有提供相同示例的教程,但它不起作用。请我在这里需要帮助..:(

ProductsHomeController.java( Controller 类)

@Controller
public class ProductsHomeController{

@RequestMapping(value = "/productsHome", method = RequestMethod.GET)
public ModelAndView welcome() {
return new ModelAndView("productsHome");
}

xml配置(MVC注解和InternalResourceViewResolver的XML配置)

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

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

web.xml

    <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext-service.xml
/WEB-INF/applicationContext-repository.xml
</param-value>
</context-param>

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


<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

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

每次我在启动时运行这个非常简单的 Web 应用程序时,Servlet通过这个URL解析 View 并显示页面的内容

  http://localhost:8080/ProductsHome/

但是当我刷新页面时,会导致 404 错误

并且我需要手动输入jsp的文件名来解析 View

  http://localhost:8080/ProductsHome/productsHome

这是我在我正在阅读的教程中经常看到的代码,尤其是 spring-recipe 书,但我无法让它按照书中所说的方式工作,我希望在第一次启动时Dispatcher-servlet 将解析 View 并显示带有以下 URL 的页面

我怎样才能做到这一点?任何帮助将不胜感激。

  http://localhost:8080/ProductsHome/productsHome/home

这是我到目前为止得到的服务器日志,我当前正在搜索需要添加到 eclipse 中以显示任何错误的内容

     Sep 04, 2014 11:37:09 AM org.apache.catalina.core.AprLifecycleListener init
INFO: Loaded APR based Apache Tomcat Native library 1.1.30 using APR version 1.4.8.
Sep 04, 2014 11:37:09 AM org.apache.catalina.core.AprLifecycleListener init
INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
Sep 04, 2014 11:37:09 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:OnlineStudentRegistration' did not find a matching property.
Sep 04, 2014 11:37:09 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:Spring.MVC' did not find a matching property.
Sep 04, 2014 11:37:10 AM org.apache.catalina.core.AprLifecycleListener initializeSSL
INFO: OpenSSL successfully initialized (OpenSSL 1.0.1g 7 Apr 2014)
Sep 04, 2014 11:37:10 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-apr-8080"]
Sep 04, 2014 11:37:10 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-apr-8009"]
Sep 04, 2014 11:37:10 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1875 ms
Sep 04, 2014 11:37:10 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Sep 04, 2014 11:37:10 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.9
Sep 04, 2014 11:37:11 AM org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [133] milliseconds.
Sep 04, 2014 11:37:14 AM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Sep 04, 2014 11:37:14 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
Sep 04, 2014 11:37:14 AM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization started
Sep 04, 2014 11:37:14 AM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Thu Sep 04 11:37:14 PDT 2014]; root of context hierarchy
Sep 04, 2014 11:37:15 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
Sep 04, 2014 11:37:16 AM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
INFO: Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.osr.controllers.StudentLoginController.student()
Sep 04, 2014 11:37:16 AM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
INFO: Mapped "{[/addStudent],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.osr.controllers.StudentLoginController.studentLogin(com.osr.domain.Student,org.springframework.validation.BindingResult)
Sep 04, 2014 11:37:17 AM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization completed in 2180 ms
Sep 04, 2014 11:37:21 AM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Sep 04, 2014 11:37:21 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Sep 04, 2014 11:37:22 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
Sep 04, 2014 11:37:23 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-apr-8080"]
Sep 04, 2014 11:37:23 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-apr-8009"]
Sep 04, 2014 11:37:23 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 13063 ms

最佳答案

我认为 Controller 应该是

@Controller
@RequestMapping(value = "/welcome")
public class WelcomeController {
@RequestMapping(method = RequestMethod.GET)
public ModelAndView welcome() {
return new ModelAndView("productsHome");
}

将欢迎文件列表放入 web.xml

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

在文件index.jsp中,WEB-INF目录下的文件index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% response.sendRedirect("welcome"); %>

更改 servlet 映射

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

确保 productsHome.jsp 位于/WEB-INF/views/目录

关于java - 无法使用 jsp 文件名作为 RequestMapping 值解析 WEB-INF/文件夹内的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25656007/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com