繁体   English   中英

Spring MVC和JSP。 基本步骤

[英]Spring MVC and JSP. Basic steps

我从Spring MVC开始,但是出了点问题,因为出现消息“请求的资源不可用”的404错误。 搜索几个小时并尝试其他操作无济于事,所以我决定向大家寻求帮助。

我有一个index.jsp,我想要一个链接,该链接将在控制器中调用该方法并返回hello.jsp。

index.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>
    <a href="${"test"}">Click to press hello</a>
</body>
</html>

通过单击此链接,我假设我应该来到我的MessageController:

package main.java.service;

//imports...

@Controller
@RequestMapping(value = "/test")
public class MessageController {

    @RequestMapping(method = RequestMethod.GET)
    public String getThis(Map<String, String> map) {
        map.put("message", "Hello World!");
        return "hello";
    }
}

根据我的理解,这应该发生,因为我标记了控制器的程序包已包含在servlet-config.xml中。

<?xml version="1.0" encoding="UTF-8"?>
<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="main.java.service"/>

   <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix" value="/WEB-INF/"/>
          <property name="suffix" value=".jsp"/>
   </bean>
 </beans>

最后是一个web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

    <display-name>SpringWebConfiguration</display-name>

    <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/servlet-config.xml</param-value>
    </context-param>
</web-app>

因此,据此,我应该得到一个带有Hello World消息的hello.jsp文件! 但是相反,我收到消息“请求的资源不可用”的404错误。 如果有人可以在这里找到问题,那就太好了。

这是我的hello.jsp

<%@ page language="java" contentType="text/html; charset=US-ASCII" pageEncoding="US-ASCII"%>
<!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=US-ASCII">
  <title>Title</title>
</head>
<body>Here there is a message: ${message}</body>
</html>

这是工作仓库: https : //github.com/vladt89/fsboard

检查href中的url是否到达您的控制器,并且hello.jsp是否在WEB-INF文件夹中可用?

将您的web.xml更改为

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

<display-name>SpringWebConfiguration</display-name>

<servlet>
    <servlet-name>spring-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/servlet-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

或者您可以通过以下方式忽略<init-param>

servlet-config.xml 重命名spring-dispatcher-servlet.xml
当不存在<init-param> Name of the Spring configuration file应与web.xml中使用的servlet-name相同,并带有-servlet servlet-name

当然,您需要将您的hello.jsp放入视图解析器中定义的WEB-INF文件夹中,

<property name="prefix" value="/WEB-INF/"/>

因此它将在WEB-INF文件夹中查找jsp文件


更新 :我已经分叉了您的代码并在我的环境中进行了测试。

您尚未在问题中发布导致此问题的代码。...太糟糕了。
在您的MessageServiceMessageServiceImpl中都添加@Component

@Component
public interface MessageService {

    void createMessage(String message);

    Collection<String> fetchAllMessages();
}

@Component
public class MessageServiceImpl implements MessageService {
    private Collection<String> messageLibrary = new LinkedList<>();

    @Override
    public void createMessage(String message) {
        messageLibrary.add(message);
    }

    @Override
    public Collection<String> fetchAllMessages() {
        return messageLibrary;
    }
}

index.jsp您的链接似乎是错误的。 您可能需要将定位标记更改为:

<a href="test">Click to press hello</a>

我要添加来自Annamalai ThangarajSotirios Delimanolis的建议。

  1. 您需要在web.xml中初始化上下文。
  2. 您需要将hello.jsp放在WEB-INF文件夹中。 这是因为您已配置的视图解析器正在WEB-INF寻找hello.jsp

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM