gpt4 book ai didi

java - spring mvc 预期输出

转载 作者:行者123 更新时间:2023-12-01 12:40:09 25 4
gpt4 key购买 nike

这是我的第一个 spring mvc 程序。该程序没有显示任何错误,但没有显示预期的输出。下面是代码..

HelloContoller.java

package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/welcome")
public class HelloController {

@RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {

model.addAttribute("message", "Spring 3 MVC 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>Message : ${message}</h1>
</body>
</html>

mvc-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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<context:component-scan base-package="com.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>


</bean>
</beans>

web.xml

    <!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-servlet.xml</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

我得到的结果为“Message : ${message}”,但我的输出应该类似于“Message : Spring 3 MVC Hello World”。这样任何人都可以帮助我找到程序中的错误。

最佳答案

您正在使用 JSP 1.2 的旧 web.xml 声明(使用 DTD 2.3)。在这种情况下,您的 EL 指令 - ${message} - 不会渲染,除非您故意启用它,默认情况下不会渲染。所以你有两个选择:

您可以使用页面指令 <%@ page isELIgnored="false" %> 来启用 EL 表达式。在 JSP 文件的 header 中。

或者您可以将 web.xml 声明升级到架构版本:

<web-app id="AppName" 
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">

...

</web-app>

如果您没有理由使用 JSP 1.2,我建议您使用第二种选择。但仍然,任何一个都应该使您的 EL 指令正确渲染。

关于java - spring mvc 预期输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25190140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com