London
London

Reputation: 15284

Spring mvc replaces quotes with html symbol

However I try to get double quotes into my view spring somehow replaces them, here is what I've tried :

@RequestMapping(value="test", method = RequestMethod.GET)
    public ModelAndView test(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("test");
        Wrapper wp = new Wrapper();
        wp.setTestField("$(function() { alert(\"test\"); });");
        mav.addObject("testObject", wp);
        return mav;
    }

Wrapper is custom object with one field testField.

@RequestMapping(value="test", method = RequestMethod.GET)
    public ModelAndView test(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("test");
        mav.addObject("testObject", "$(function() { alert(\"test\"); });");
        return mav;
    }

And jsp :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>
<script type="text/javascript">
<c:out value="${requestScope.testObject.testField}"></c:out>
</script>
</body>
</html>

Result is :

<script type="text/javascript">
    alert(&#039;test&#039;);    
</script>

I want to get :

 <script type="text/javascript">
        alert("test");    
    </script>

Upvotes: 1

Views: 1628

Answers (1)

skaffman
skaffman

Reputation: 403581

That's because <c:out> automatically escapes your content.

To stop it doing that, use

<c:out escapeXml="false" value="${requestScope.testObject.testField}"/>

Upvotes: 4

Related Questions