JSTL 问题(春季 3)

发布于 2024-12-23 16:22:27 字数 4281 浏览 0 评论 0 原文

我正在尝试让我的 Spring 应用程序正常工作,跟随一个发布了早期 Spring 版本示例的人。我对他的例子有很多问题,所以我没有发布链接,以免让其他人误入歧途。经过与问题的斗争,我终于能够让事情顺利进行。有些。我无法弄清楚以下两件事:

  1. $(message) 没有打印消息,该消息是 String 类型的 bean。
  2. 打印消息,但前提是我添加 <%@taglib prefix="c" uri="http://java.lang. sun.com/jsp/jstl/core" %> 到 jsp。我认为将 jtslview 类添加到解析器就足够了。它不是。这是怎么回事?

设置

  Eclipse IDE for Java EE Developers    1.4.1.20110909-1818 epp.package.jee 
  Tomcat 7
  Spring 3 distribution (all jars in /lib)
  lib/jstl-1.2.jar

有问题的 jsp

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ 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>Message View</title> 
</head>
<body>
    $(message)
    <br>
    message=$(message);
    <br>
    message=<c:out value="${message}" />
</body>
</html>

会打印以下内容。消息“bean”被清楚地传递到页面。

$(message) 
message=$(message); 
message=Hello World from Spring MVC! 

网络.xml

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

        <display-name>Spring3MVC</display-name>    
        <welcome-file-list>        
            <welcome-file>index.jsp</welcome-file>    
        </welcome-file-list>     

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

        <servlet-mapping>        
            <servlet-name>spring</servlet-name>        
            <url-pattern>*.html</url-pattern>    
        </servlet-mapping>

</web-app> 

spring-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:p="http://www.springframework.org/schema/p"    
    xmlns:context="http://www.springframework.org/schema/context"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">     

    <context:component-scan base-package="com.helloworldexample.controllers" />     

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

控制器(以防万一)

package com.helloworldexample.controllers;

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class MesssageController {


@RequestMapping("/hello")
public ModelAndView handleHello() {
    String message="Hello World from Spring MVC!";

    return new ModelAndView("messageView", "message", message);

}

@RequestMapping("/welcome.html")
public ModelAndView handleWelcome() {
    String message="Welcome in Spring MVC!";

    return new ModelAndView("messageView", "message", message);

}

}

I'm trying to get my spring application working, following a guy who posted an example with earlier spring version. I've been having many problems with his example so I'm not posting a link so as not to lead anyone else astray. Fighting through issues, I was able to get to the point where things are working. Somewhat. I have not been able to figure out the following two things:

  1. $(message) isn't printing the message which is a bean of type String.
  2. <c:out value="${message}" prints the message but only if I add <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> to the jsp. I would assume that adding jtslview class to the resolver would be enough. It's not. What's wrong here?

setup

  Eclipse IDE for Java EE Developers    1.4.1.20110909-1818 epp.package.jee 
  Tomcat 7
  Spring 3 distribution (all jars in /lib)
  lib/jstl-1.2.jar

jsp in question

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ 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>Message View</title> 
</head>
<body>
    $(message)
    <br>
    message=$(message);
    <br>
    message=<c:out value="${message}" />
</body>
</html>

this prints the following. The message "bean" is clearly passed to the page.

$(message) 
message=$(message); 
message=Hello World from Spring MVC! 

web.xml

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

        <display-name>Spring3MVC</display-name>    
        <welcome-file-list>        
            <welcome-file>index.jsp</welcome-file>    
        </welcome-file-list>     

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

        <servlet-mapping>        
            <servlet-name>spring</servlet-name>        
            <url-pattern>*.html</url-pattern>    
        </servlet-mapping>

</web-app> 

spring-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:p="http://www.springframework.org/schema/p"    
    xmlns:context="http://www.springframework.org/schema/context"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">     

    <context:component-scan base-package="com.helloworldexample.controllers" />     

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

controller (just in case)

package com.helloworldexample.controllers;

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class MesssageController {


@RequestMapping("/hello")
public ModelAndView handleHello() {
    String message="Hello World from Spring MVC!";

    return new ModelAndView("messageView", "message", message);

}

@RequestMapping("/welcome.html")
public ModelAndView handleWelcome() {
    String message="Welcome in Spring MVC!";

    return new ModelAndView("messageView", "message", message);

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

開玄 2024-12-30 16:22:27

JSP 表达式语言的语法是${some expression}。不是$(某些表达式)

每次使用 JSP 标记时,必须在 JSP 中声明其标记库。您将 Spring 配置为使用 JstlView 的事实与使用 标记的可能性无关。它只是配置 Spring 在分派到视图之前执行适当的操作。

应始终用于呈现字符串,除非您完全确定该字符串不包含任何必须进行 HTML 转义的字符。如果不使用它,就会遭受攻击,用户可能会提交包含 的文本或一段 JavaScript 代码,从而危及您的网站。

阅读有关 servlet 和 JSP 的官方教程:http://docs.oracle。 com/javaee/5/tutorial/doc/bnadp.html

The syntax for the JSP expression language is ${some expression}. Not $(some expression).

Each time you use a JSP tag, its tag library must be declared in the JSP. The fact that you configure Spring to use a JstlView has nothing todo with the possibility of using the <c> tags. It just configures Spring to perform appropriate actions before dispatching to the view.

<c:out> should always be used to render a string, unless you're absolutely sure that the string doesn't contain any character that must be HTML-escaped. Not using it opens the door to attacks where a user could submit a text containing </html> or a piece of javascript code that would compromise your web site.

Read the official tutorial about servlets and JSPs: http://docs.oracle.com/javaee/5/tutorial/doc/bnadp.html

尐籹人 2024-12-30 16:22:27

也许您需要将导入标签库更改

  <%@ taglib prefix="c" uri="htttp://java.sun.com/jsp/jstl/core" %>
  <%@ taglib prefix="fmt" uri="htttp://java.sun.com/jsp/jstl/fmt" %>

  <%@ taglib prefix="c" uri="htttp://java.sun.com/jstl/core" %>
  <%@ taglib prefix="fmt" uri="htttp://java.sun.com/jstl/fmt" %>

Perhaps you need to change your import tagslib

  <%@ taglib prefix="c" uri="htttp://java.sun.com/jsp/jstl/core" %>
  <%@ taglib prefix="fmt" uri="htttp://java.sun.com/jsp/jstl/fmt" %>

to

  <%@ taglib prefix="c" uri="htttp://java.sun.com/jstl/core" %>
  <%@ taglib prefix="fmt" uri="htttp://java.sun.com/jstl/fmt" %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文