gpt4 book ai didi

java - spring mvc页面无绑定(bind)

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

我写了一个非常简单的 spring-mvc 示例,期望在页面中显示“hello world”。但它失败了。它仅在页面中显示“${message}”

下面是我的代码和jsp。我有什么错误吗?或者我错过了什么配置?谢谢!

代码:Java:

    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 HomepageController extends AbstractController {

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



ModelAndView mav = new ModelAndView();
mav.addObject("message", "hello world");
mav.setViewName("homepage");
return mav;
}

}

servlet.xml:

    <?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">


<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/home.htm">homepageController</prop>
</props>
</property>
</bean>

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>


<bean id="homepageController" class="com.sunjq.config.web.HomepageController"/>



</beans>

web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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 web-app_2_4.xsd"
version="2.4">

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

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/serviceContext.xml
/WEB-INF/daoContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>


<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/appServlet-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

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


<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>



</web-app>

jsp页面:

  <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Homepage</title>
</head>
<body>
${message}
</body>
</html>

最佳答案

我猜你没有启用jSTL,这与spring mvc无关。
您需要:

  1. 确保您的 Web 容器已安装 jSTL
  2. 在页面顶部添加以下内容

以下答案提供了有关此主题的良好信息 Enabling JavaServerPages Standard Tag Library (JSTL) in JSP我建议将其作为引用。

关于java - spring mvc页面无绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20040998/

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