【问题标题】:Spring mvc - initial loading page issueSpring mvc - 初始加载页面问题
【发布时间】:2018-05-12 17:03:04
【问题描述】:

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

<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

【问题讨论】:

  • 更新 dispatcherservlet 的 url 模式
  • 你好yati,我不能关注你,你能举个例子吗?
  • 在下面查看我的答案

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


【解决方案1】:

healthTracker 是您的根上下文。

您的控制器路径可通过/greeting 访问:

@RequestMapping(value ="greeting")

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

http://localhost:1615/healthTracker/greeting

【讨论】:

  • 您好,我尝试使用localhost:1615/healthTracker/greeting 访问,但它仍然显示错误。类型异常报告消息 Servlet.init() for servlet [healthTrackerServlet] 抛出异常说明服务器遇到意外情况阻止它从满足请求。异常 javax.servlet.ServletException: Servlet.init() for servlet [healthTrackerServlet] 引发异常 根本原因 java.lang.IllegalArgumentException
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    如果您希望hello.jsp 作为默认值,您必须在 index.jsp 中保留一些 sn-p 保持&lt;jsp:forword page="greeting"&gt;

    并在 web.xml 中更改为

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

    【讨论】:

      猜你喜欢
      • 2010-10-30
      • 1970-01-01
      • 1970-01-01
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多