转义textarea中的XML标记

时间:2016-10-19 07:21:08

标签: java xml jsp spring-boot escaping

我正在使用jsp进行spring boot,我需要在jsp textarea中显示XML字符串。我在显示XML时遇到了一个奇怪的问题。第一个标签刚刚被删除,最后添加了">

Note.xml

<?xml version="1.0" encoding="UTF-8"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

greetings.jsp

<!DOCTYPE html>

<html>
    <head>            
        <meta charset="UTF-8">
        <title>Hello</title>
    </head>
    <body>
        <form action="/smartxml/openxml" method="POST" enctype="multipart/form-data">
            <input type="file" name="file">
            <input type="submit" value="Open" name="btnOpen" /><br/><br/>
            <textarea name="txtXml" rows="45" cols="223" value="${fileContent}"></textarea>
        </form>
    </body>
</html>

IndexController.java

    @Controller
    public class IndexController {
    @RequestMapping(params = "btnOpen", method = RequestMethod.POST)
        public String uploadFile(@RequestParam("file") MultipartFile file, Model model) {
            try {
                InputStream is = file.getInputStream();
                StringWriter writer = new StringWriter();
                IOUtils.copy(is, writer, StandardCharsets.UTF_8);
                String fileContent = writer.toString();
                model.addAttribute("fileContent", xmlOperation.readXml(is));
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }

            return "greeting";
        }
    }

输出

<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>">

任何帮助将不胜感激..

1 个答案:

答案 0 :(得分:1)

<textarea name="txtXml" rows="45" cols="223">${fileContent}</textarea>