1
但是我試圖讓雙引號到我看來春天莫名其妙地替換它們,這裏是我已經試過:Spring MVC的替換引號與HTML符號
@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;
}
包裝是一個場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;
}
和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>
結果是:
<script type="text/javascript">
alert('test');
</script>
我想:
<script type="text/javascript">
alert("test");
</script>
或者只是使用'<腳本類型= 「文/ JavaScript的」> $ {} testObject.testField'當你已經在2.4的Servlet/JSP 2.0或更新(這可能適用於基於Spring MVC的web應用程序)。 – BalusC 2011-05-12 18:23:33
@BalusC:的確如此,但我的選擇是將其明確。 – skaffman 2011-05-12 18:24:09