java - 即使定义了请求映射,为什么请求调度程序仍在 spring mvc 中查找 View ?

标签 java spring spring-mvc

我有一个spring mvc应用程序,配置如下。我已经正确配置了 spring 上下文、调度程序 servlet 和注释处理。我仍然遇到这个问题,即使我定义了请求映射,spring 仍在尝试查找 JSP,并且出现以下错误

HTTP Status 404 - /resp/WEB-INF/views/public_html.jsp

但是,我能够成功调用其他请求映射(主要是 POST)。请参阅下面的配置信息。欢迎所有建议。

网络 XML

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/springApplicationConfig-servlet.xml</param-value>
</context-param>
<context-param>
    <param-name>viewsLocation</param-name>
    <param-value>/WEB-INF/views/</param-value>
</context-param>


<servlet>
    <servlet-name>springApplicationConfig</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springApplicationConfig</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

springApplicationConfig-servlet.xml

<context:annotation-config/>
<context:component-scan base-package="com.pkgName" />

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

<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**/*" /> 
        <bean id="webContentInterceptor"
            class="org.springframework.web.servlet.mvc.WebContentInterceptor">
            <property name="cacheSeconds" value="0" />
            <property name="useExpiresHeader" value="true" />
            <property name="useCacheControlHeader" value="true" />
            <property name="useCacheControlNoStore" value="true" />
        </bean>
    </mvc:interceptor>
</mvc:interceptors>

Controller

@Controller
public class LandingPageController {


@RequestMapping(value = "/" , method = RequestMethod.GET)
public ModelAndView homePage(HttpServletRequest request) {

    return new ModelAndView("index", "model", null);
}

@RequestMapping(value = "/public_html", method = RequestMethod.GET)
public ModelAndView homePagePub(HttpServletRequest request) {

    return new ModelAndView("redirect:/");
}}

提前致谢!

最佳答案

让我们复兴这个......:)

这应该有效......但无论如何你还有其他选择可以尝试。 您可以返回一个 RedirectView,或者只是返回一个 String 'redirect:/'

//...
@RequestMapping(value = "/public_html", method = RequestMethod.GET)
public View homePagePub() {
    return new RedirectView("/", true);
}
// - OR -
@RequestMapping(value = "/public_html", method = RequestMethod.GET)
public String homePagePub() {
    return "redirect:/";
}
//...

关于java - 即使定义了请求映射,为什么请求调度程序仍在 spring mvc 中查找 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31559230/

相关文章:

java - Arduino Ping))) 传感器在显示一些结果后突然停止

java - JPanels 无法设置任何大小值

java - jetty :run fails with NoSuchMethodError with Spring 5

java - Spring Boot - 如何从 file.conf 读取环境变量

java - 给定日期的正确日期格式是什么

java - 用Maven目录结构的Maven工程源码生成jar?

java - Spring Boot MongoRepository 忽略验证

java - Spring 4 - addResourceHandlers 不解析静态资源

java - spring-boot中的过滤顺序

java - Spring MVC 生成带引号的字段名称 json 输出