在浏览器上使用spring mvc时,无法显示servlet发送的数据

时间:2018-06-30 20:03:27

标签: java spring-mvc

有人可以帮助我进行以下查询。 当我在浏览器上打开URL http://localhost:8082/FirstSpring/HelloPage.jsp时,servlet发送的数据{$ welcomeMessage}不会被打印。

我已经在此站点上尝试过一种类似类型的上一个问题(使用spring mvc在浏览器上显示欢迎消息)中的某些选项,但似乎对我没有任何作用。

Below is my code for diff files.

web.xml file

`<web-app id = "WebApp_ID" version = "2.4"
   xmlns = "http://java.sun.com/xml/ns/j2ee" 
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

   <display-name>FirstSpring</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>

</web-app>
`
----------------------------------------------------------------------------

Servlet file(spring-dispatcher-servlet.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"
    xsi:schemaLocation="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-4.3.xsd">

<context:component-scan base-package="com.springtraining.HelloController"/>


<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix">
<value>/WEB-INF/views/</value></property>
<property name="suffix">
<value>.jsp</value></property>
</bean>

</beans>

----------------------------------------------------------------------------

Controller class(HelloController.java)

`package com.springtraining;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;


import org.springframework.web.servlet.mvc.AbstractController;

public class HelloController extends AbstractController{

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        ModelAndView modelandview = new ModelAndView("HelloPage");
        modelandview.addObject("welcomeMessage", "Hi User, welcome to the first Spring MVC Application");
        return modelandview;
    }
}`
--------------------------------------------------------------------------------
jsp file(HelloPage.jsp)

`<html>
<body>
    <h1>First Spring MVC Application Demo</h1>

    <h2>${welcomeMessage}</h2>

</body>
</html>`

-----------------------------------------------------------------------------

如果有人可以帮助我,那将非常好。

先谢谢了。

0 个答案:

没有答案