Reputation: 2242
In my JSP page,I use <%=TITLE %>
to show the page title, sometimes it is ok.But sometimes the page show that can not cpmplie the code <%=TITLE %>
.
So I change the code to ${TITLE}
,and it is OK.What is different between <%=TITLE %>
and ${TITLE}
in jsp?
Here is my page code:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<base href="<%=basePath%>">
<title><%=TITLE %></title>
<meta name="description" content="${DESCRIPTION}">
<meta name="keyword" content="${KEYWORD}">
</head>
I define them in the controller:
ModelAndView mv = this.getModelAndView();
mv.addObject("DESCRIPTION","MYDESCRIPTION"));
mv.addObject("KEYWORD","MYKEYWORD");
mv.addObject("TITLE","MYTITLE");
return mv;
Upvotes: 0
Views: 474
Reputation: 3274
Answer according to context of your Question
What is different between “<%=TITLE %> ” and “${TITLE} ” in jsp?
Since most of the times we print dynamic data in JSP page using out.print() method, there is a shortcut to do this through JSP Expressions. JSP Expression starts with <%= and ends with %>.
<% out.print(TITLE); %>
above statement is called scriptlet can be written using JSP Expression as
<%= TITLE %>
We can use scriptlets and JSP expressions to retrieve attributes and parameters in JSP with java code and use it for view purpose. But for web designers, java code is hard to understand and that’s why JSP Specs 2.0 introduced Expression Language (EL) through which we can get attributes and parameters easily using HTML like tags.
Expression language syntax is
${TITLE}
and we can use EL implicit objects and EL operators to retrieve the attributes from different scopes and use them in JSP page.
According to your controller related query
I say to write like this
<title><%=request.getAttribute("TITLE"); %></title>
because it's stored as a request attribute.
NOTE
Scriptlets are discouraged since JSP 2.0 which was released almost a decade(!) ago. So Please use Expression Language (EL).
Upvotes: 2