我试图将hello.jsp页面作为输出,但它总是转到index.jsp页面。我试着搜索各种其他的东西,但失败了,所以最后决定在这里张贴,以便任何一个你可以帮助。下面我附上了我的项目结构和所有的文件......以及我在Tomcat服务器上运行时所看到的输出。
Project Structure
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gami</groupId>
<artifactId>healthTracker</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>healthTracker Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>healthTracker</finalName>
</build>
</project>
Web.xml
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
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">
<servlet>
<servlet-name>healthTrackerServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servlet-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>healthTrackerServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<display-name>Archetype Created Web Application</display-name>
</web-app>
servlet-config
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.gami.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> -->
</beans>
HelloController.java
package com.gami.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping(value ="greeting")
public String sayHello (Model model) {
model.addAttribute("greeting", "Hello World");
return "hello";
}
hello.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>${greeting}</h1>
</body>
</html>
index.jsp
<html>
<body>
<h2>This is index - Hello World!</h2>
</body>
</html>
This is the output I get when I run the project on server
These are logs, server was started properly
4条答案
按热度按时间bjp0bcyl1#
healthTracker
是根上下文。您的控制器路径可在
/greeting
访问:所以如果你想访问hello页面,你的url应该是:
wnvonmuf2#
在部署描述符(web.xml)中更新
servlet-mapping
首先,检查您的服务器是否运行在
1615
上如果是,那么
用于访问资源的基本URL的格式如下
所以用途:
http://localhost:1615/healthTracker/greeting
另外,建议您在
@RequestMapping
中指定请求方法,默认为GET如果要使某个页成为应用程序的默认页。还有另一种方法,只需在
web.xml
中指定页面即可5fjcxozz3#
如果你想让
hello.jsp
作为默认值,你必须在index.jsp中保留一些代码片段。并在web.xml中更改为
0dxa2lsx4#
您可以尝试以下方法来解决此问题。
简单地说,您的Web应用程序将查看
welcome page
,并调用/redirect
,这是由controller
类捕获的,并执行您实现的逻辑。在
web.xml
中添加此内容在
JSP
本身中,添加以下编码:最后,在控制器中: