【问题标题】:local server not able to find the welcome.jsp file in sprin web mvc本地服务器无法在 spring web mvc 中找到 welcome.jsp 文件
【发布时间】:2019-02-01 08:09:53
【问题描述】:

Project link

我正在使用 maven 和 tomcat 服务器做一个 spring web-mvc 应用程序。所以问题是点击index.jsp的链接后,并没有重定向到welcome.jsp。

controller class

package com.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

public class controller {

@RequestMapping("/welcome")
 public String redirect()  
{  
    return "welcome";  
}  }

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance..." 
version="4.0">
<display-name>CrunchifySpringMVCTutorial</display-name>

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

index.jsp

<!DOCTYPE html >
<html><head><title>Spring MVC Tutorial Series by Crunchify.com</title>
<h3><a href="welcome">Click here to See Welcome Message... /></h3>
</div></body></html>

Welcome.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "ht
<html><head>
<title>Spring MVC Tutorial by Crunchify - Hello World Spring MVC
Example</title></head><body>

Spring MCV Tutorial by <a href="https://crunchify.com">Crunchify</a>.
Click <a href="https://crunchify.com/category/java-tutorials/"
target="_blank">here</a> for all Java and <a
href='https://crunchify.com/category/spring-mvc/' 
target='_blank'>here</a>
for all Spring MVC, Web Development examples.<br>
</div></body></html>

crunchify-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=..."

<mvc:annotation-driven />
<context:component-scan
base-package="com.controller" />
<mvc:default-servlet-handler />
<bean id="viewResolver" 
class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
<property name="prefix" value="/WEB-INF/JSP/"></property>  
<property name="suffix" value=".jsp"></property>          
</bean> </beans>

这是我的目录结构。我不知道如何去welcome.jsp。

有人可以帮助我吗?为什么它没有重定向到welcome.jsp??

【问题讨论】:

  • XML view Resolver 请看这个教程,也许你会得到一些帮助。
  • 您需要将@Controller注解添加到您的控制器类,否则它不会被组件扫描&lt;context:component-scan base-package="com.controller" /&gt;拾取,因此不会注册到Spring MVC框架。
  • 嗨@AlanHay,我错过了。谢谢你告诉我,但结果是一样的。你能再告诉我哪里出错了吗??

标签: spring spring-mvc jsp tomcat


【解决方案1】:

问题在于您的 index.jsp

<h3><a href="welcome">Click here to See Welcome Message... /></h3>

您在anchor 中直接提到welcome,这在此处不起作用。

使用就是这样

  • 使用&lt;form action="page2.jsp"&gt;

提交给一个servlet使用

<form action="targetServlet">, 

然后:

重定向到第2页,使用response.sendRedirect("page2.jsp")

使用request.getRequestDispatcher("page2.jsp").forward()转发到第2页

【讨论】:

    猜你喜欢
    • 2014-09-04
    • 2021-03-25
    • 1970-01-01
    • 2015-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    相关资源
    最近更新 更多