Spring mvc - 初始加载页面问题

时间:2018-05-12 17:03:05

标签: java spring spring-mvc event-handling requestdispatcher

我正在尝试将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

3 个答案:

答案 0 :(得分:0)

healthTracker是您的根背景。

您可以在/greeting

访问您的控制器路径
@RequestMapping(value ="greeting")

因此,如果您想访问hello页面,您的网址应为:

http://localhost:1615/healthTracker/greeting

答案 1 :(得分:0)

更新部署描述符(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>  

答案 2 :(得分:0)

如果您希望hello.jsp作为默认值,则必须在index.jsp中保留一些代码段 保持<jsp:forword page="greeting">

并在web.xml中更改为

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

</servlet-mapping>