Spring mvc -初始加载页面问题

vc9ivgsu  于 2023-05-07  发布在  Spring
关注(0)|答案(4)|浏览(205)

我试图将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

bjp0bcyl

bjp0bcyl1#

healthTracker是根上下文。
您的控制器路径可在/greeting访问:

@RequestMapping(value ="greeting")

所以如果你想访问hello页面,你的url应该是:

http://localhost:1615/healthTracker/greeting
wnvonmuf

wnvonmuf2#

在部署描述符(web.xml)中更新servlet-mapping

<servlet-mapping>
        <servlet-name>healthTrackerServlet</servlet-name>
        <url-pattern>/*</url-pattern>

</servlet-mapping>

首先,检查您的服务器是否运行在1615
如果是,那么
用于访问资源的基本URL的格式如下

https://<IP>:<Listener-PORT>/<ROOT-CONTEXT-NAME>/<URL-SPECIFIED-FOR-RESOURCE>

所以用途:http://localhost:1615/healthTracker/greeting
另外,建议您在@RequestMapping中指定请求方法,默认为GET

@RequestMapping(value ="greeting" , method = RequestMethod.POST)
    public String sayHello (Model model) {

        model.addAttribute("greeting", "Hello World");

        return "hello";
    }

如果要使某个页成为应用程序的默认页。还有另一种方法,只需在web.xml中指定页面即可

<web-app>  
 ....  

  <welcome-file-list>  
   <welcome-file>hello.html</welcome-file>  
  </welcome-file-list>  
</web-app>
5fjcxozz

5fjcxozz3#

如果你想让hello.jsp作为默认值,你必须在index.jsp中保留一些代码片段。
并在web.xml中更改为

<servlet-mapping>
        <servlet-name>healthTrackerServlet</servlet-name>
        <url-pattern>/*</url-pattern>

</servlet-mapping>
0dxa2lsx

0dxa2lsx4#

您可以尝试以下方法来解决此问题。
简单地说,您的Web应用程序将查看welcome page,并调用/redirect,这是由controller类捕获的,并执行您实现的逻辑。
web.xml中添加此内容

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

JSP本身中,添加以下编码:

<c:redirect url="/directMe"/>

最后,在控制器中:

@RequestMapping(value = "/directMe", method = RequestMethod.GET)
        public void myMethod(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException { 
// add your logic to execute
}

相关问题