5

I am doing a Spring web application. In my messages.properties file, there is a one-line string as follows:

label.name.tooltip=The "name" field ...

My JSP file displays this string as follows:

<spring:message code="label.name.tooltip" />

However, the displayed text is only "The", which means the part from "name" is cut.

I dont know why this happens. Googled and the ways such as adding a backslash before double quotes are not working.

Regards and thanks!

Update

The whole problem was caused by me using the string in the title attribute of A tag as follows:

a href="#" data-toggle="tooltip" title="<spring:message code="label.name.tooltip" />

As Bossie hinted, the browser removes the string content starting from the double quote.

Misha did quite an explanation, which helped me to understand more about how the message works. Thanks, Misha!!!

I found a solution at SO that in my case, use "name" instead of double quotes. Hope this helps someone else.

curious1
  • 14,155
  • 37
  • 130
  • 231

4 Answers4

4

Spring resolves message codes using a MessageSource, which in turn uses a MessageSourceSupport object. If your message is the victim of the application of MessageFormat rules (defined here), you can just escape the double quotes using some single quotes:

The '"'name'"' field should not be null, empty, or start with an integer.

Unfortunately, MessageFormat doesn't state that double quotes are special characters, so this is really just a shot in the dark.

Edit:

Alright. I went and tried it. I have no problems loading double quotes as-is. My messages.properties has one line:

test.message = "Hello, world!"

And my test looks like this:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestConfiguration.class)
public class TestQuotes {

    @Autowired
    private MessageSource source;

    @Test
    public void test() {
        assertEquals(
            "\"Hello, world!\"", 
            source.getMessage("test.message", null, Locale.US));
    }
}

Therefore, the only conclusion is that the .jsp processor consumes your double quotes - it's definitely not the message source-related objects. I don't know what object renders your .jsp, but you should take a closer look to see what rules it has about parsing injected values (and maybe try double-double quotes or something).

Misha
  • 909
  • 1
  • 7
  • 15
  • Hi Misha, thanks sooooooooo much for helping me out. I tried your single quote escape but it is not working. I updated my post. – curious1 May 31 '14 at 05:53
  • I did a brief test, and the message resolution itself for double quotes seems to work (just confirmed that it's not a message source problem). It's almost definitely whatever is injecting that string into the JSP. – Misha May 31 '14 at 06:15
  • 1
    Or the browser doesn't display the string properly because the double quotes mess up the HTML. Does it look okay when you click "View source" (or whatever the equivalent is in your browser) ? – Jan Van den bosch May 31 '14 at 06:18
  • I didn't ask it to render to a JSP. Check out [this](http://stackoverflow.com/questions/15135769/how-to-code-double-quotes-via-html-codes) question and maybe replace your double quotes if you expect a browser to render them correctly. – Misha May 31 '14 at 06:19
  • Misha, thanks so much for confirming it is not a Spring issue. Thanks for your tests and explanation. Your test really points me to think about something else. And indeed it is something else. – curious1 May 31 '14 at 06:48
  • Bossie, thanks very much for pointing this out. In JSP, I put this string in the title attribute of A tag for the purpose of tooltip. When I take it out to display on a JSP page, the double quotes are shown correctly as Misha confirmed. – curious1 May 31 '14 at 06:50
0

Using HTML escape characters instead of the double quotes in the properties file might help: http://www.theukwebdesigncompany.com/articles/entity-escape-characters.php

So in your case something like:

label.name.tooltip=The &#34;name&#34; field ...
theyuv
  • 1,556
  • 4
  • 26
  • 55
0

Spring message gives a way to escape messages that contains double quotes, new line etc.
Just put the following in HTML an document:

<a href="#" title="<spring:message htmlEscape='true' code='label.myLabel'/>" />

Or put the following in a Javascript block:

<script>
    function myFunction() {
        alert("<spring:message javaScriptEscape='true' code='label.myLabel' />");
    }
</script>

In this way it is not necessary to put HTML escape characters or something else in the messages.properties file.

For more details visit this post

fl4l
  • 1,580
  • 4
  • 21
  • 27
0

For this you can use "\" in Application properties

files.check={hello:'\"hi\"',bello:'bye'}

Controller code:

@Value("#{${files.check}}")
private Map<String, String> propertyname;

@GetMapping("/check")
public void authUser() {
        System.err.println(propertyname);
}

output: {hello="hi", bello=bye}