本地服务器无法在sprin web mvc中找到welcome.jsp文件

时间:2019-02-01 08:09:53

标签: spring spring-mvc jsp tomcat

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。 Directory Strucutre

enter image description here

enter image description here

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

1 个答案:

答案 0 :(得分:0)

问题出在您的index.jsp

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

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

使用就是这样

  • 使用<form action="page2.jsp">

使用

提交到servlet
<form action="targetServlet">, 

然后:

使用response.sendRedirect("page2.jsp")

重定向到page2

使用request.getRequestDispatcher("page2.jsp").forward()

转到第2页