Set locale for JSP page in Openfire - java

I have a plugin for Openfire. There is a JSP page inside it, and I want the locale on this page to be different from the global locale.
I tried this:
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%#taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%
String locale = ParamUtils.getParameter(request, "locale");
%>
<html>
<head>
<title>Page</title>
</head>
<body>
<fmt:setLocale value="<%=locale%>" scope="session"/>
<fmt:message key="hello.world"/> <br>
</body>
</html>
But it doesn't work.
When I switch the global locale, my JSP page shows me right language. But I don't want to change the global locale, I only want to change it for this page.
What can I do to make this work?

Related

JSP adds HTML to the view but doesn't execute it to the user

I wanted to insert html code to a jsp page so I used normal spring controller populated my model with html items, then once I start to render the data on the view ,it show the user a row html tags rather than an actual elements like:
<p> <strong> Description:</strong></p>
I wanted to show the user an actual strong text not the tag itself ,anyone knows how to achieve that?
my view is like that:
<%# page isELIgnored ="false" %>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
<c:out value="${Description}" escapeXml="false" />
</body>
</html>
any Idea how to solve it?
Edit : part of the code where I send the html :
model.addAttribute("Description", jobpost.getDescription()
.replace("<", "<")
.replace(">", ">")
.replace("&", "&")
.replace(""", "\"")
.replace("&apos;", "\\"));
Edit 2 : it finally worked guys It was a problem with the above code I forgot to insert ; at the end of & lt;
You can try wrapping the field with <b> or <h2>tag
<%# page isELIgnored ="false" %>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
<b><c:out value="${Description}" escapeXml="false" /></b> // like this
</body><
/html>

Google App Engine: Get source link to entity property

I'm new to Google App Engine so I was hoping that you could help me here.
I'm trying to get the source link to a property of an entity (want to download a json), but can't figure out how.
This is the code:
<%-- //[START all]--%>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<%# page import="com.google.appengine.api.users.User" %>
<%# page import="com.google.appengine.api.users.UserService" %>
<%# page import="com.google.appengine.api.users.UserServiceFactory" %>
<%-- //[START imports]--%>
<%# page import="com.google.appengine.api.datastore.DatastoreService" %>
<%# page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %>
<%# page import="com.google.appengine.api.datastore.Entity" %>
<%# page import="com.google.appengine.api.datastore.FetchOptions" %>
<%# page import="com.google.appengine.api.datastore.Key" %>
<%# page import="com.google.appengine.api.datastore.KeyFactory" %>
<%# page import="com.google.appengine.api.datastore.Query" %>
<%-- //[END imports]--%>
<%# page import="java.util.List" %>
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<html>
<head>
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Mandatory Assigment 2</title>
</head>
<body>
<h1>Mandatory Assignment 2</h1>
<p>This page shows the CSV files uploaded</p>
<h2>Uploaded CSV files</h2>
<%-- //[START datastore]--%>
<%
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Key guestbookKey = KeyFactory.createKey("Guestbook", "guestbookName");
// Run an ancestor query to ensure we see the most up-to-date
// view of the Greetings belonging to the selected Guestbook.
Query query = new Query("Greeting", guestbookKey).addSort("date", Query.SortDirection.DESCENDING);
List<Entity> greetings = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(5));
if (greetings.isEmpty()) {
%>
<p>There are no CSV files. Please refresh to reload</p>
<%
} else {
%>
<p>CSV files: </p>
<%
for (Entity greeting : greetings) {
pageContext.setAttribute("greeting_content",
greeting.getProperty("content"));
if (greeting.getProperty("user") == null) {
%>
<p>An anonymous person wrote:</p>
<%
} else {
pageContext.setAttribute("greeting_user",
greeting.getProperty("user"));
%>
<p>json string: ${fn:escapeXml(greeting_content)}</a></a></p>
<%
}
%>
<blockquote></blockquote>
<%
}
}
%>
</body>
</html>
<%-- //[END all]--%>
The "${fn:escapeXml(greeting_content)}" code outputs the json as a String, but I would like to be able to download the json instead like this:
<p>download JSON</a></p>
Can someone help me? I'd appreciate it!
Try this code:
<p>download JSON</a></p>
Adding download attribute you can make content downloadable in the format you specified

Difference between <html:html></html:html> and <html></html>

Good day all,
I saw <html:html></html:html> from a jsp page in a java project.
Would like to ask what is the difference between these html tags.
Kindly advise.
The example code is as follow:
<%# taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%# taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<html:html>
<head>
</head>
<body>
<!-- some html code here -->
</body>
</html:html>
<html:html> uses the struts-html tag library, where <html></html> is just plain old html.
You can read all about the struts-html taglib here.
both are same.html:html is struts 1 tag which is equal to basic HTML's html tag.

Spring model attribute/jsp not visible

Learning spring + jsp.
I am setting a model attribute in a method in a controller
#RequestMapping("/viewExpenses")
public String viewAllExpenses(Model model){
List<Expense> expenses;
expenses = expenseService.getAllExpenses();
model.addAttribute(expenses);
for(Expense e : expenses)
System.out.println(e);
return "viewExpenses";
}
where expenses size is > 1
Following is my jsp page:
<%# taglib prefix="s" uri="http://www.springframework.org/tags"%>
<%# taglib prefix="sf" uri="http://www.springframework.org/tags/form"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home Page</title>
</head>
<body>
<h2>
Hello ${user.firstName} <br> HOME
</h2>
<div>All Expenses</div>
<span style=""></span>
<c:forEach var="expense" items="${expenses}">
<c:out value="${expense.expenseId}"></c:out>
<c:out value="${expense.expenseName}"></c:out>
<c:out value="${expense.expenseType}"></c:out>
<c:out value="${expense.expensePrice}"></c:out>
<c:out value="${expense.purchaseDate}"></c:out>
<c:out value="${expense.expenseComments}"></c:out>
</c:forEach>
</body>
</html>
Following is my output from the jsp(no errors):
Hello Pravat
HOME
All Expenses
I wonder why the expenses are not populated in my jsp
You need to give your attribute an attribute name:
model.addAttribute("expenses", expenses);
Background:
When a name is not supplied, Spring uses a generated name which is not the same as the variable name. The docs explain this:
Determine the conventional variable name for the supplied Object based on its concrete type. The convention used is to return the uncapitalized short name of the Class, according to JavaBeans property naming rules: So, com.myapp.Product becomes product; com.myapp.MyProduct becomes myProduct; com.myapp.UKProduct becomes UKProduct.
Give it a name
model.addAttribute("expenses", expenses);
Return ModelAndView.
Always enclose body of if/for/etc into { } even it's single-lined.

JSTL - Output not coming as expected

Below is the code I have in index.jsp using jstl 1.2.
<%# taglib prefix = "c" uri="http://java.sun.com/jstl/core"%>
<% String[] setName = {"Hello", "you", "are", "using", "jstl", "in", "jsp"};
request.setAttribute("getName", setName);
%>
<html>
<body>
<table>
<tr><td>Print</td></tr>
<c:forEach var="itemName" items="#{getName}" >
<tr>
<td>${itemName}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
The output I was expecting is as below
Print
Hello
you
are
using
jstl
in
jsp
However below is what I am getting
Print
#{name}
Please let me know where I am missing
Below is the only jar file I have in WEB-INF/lib folder
jstl-1.2.jar
Thanks in advance
Fahim
Note: Adding Java and JSP tag as person who have knowledge of Java and JSP might be knowing JSTL too...
Here,
<%# taglib prefix = "c" uri="http://java.sun.com/jstl/core"%>
You're specifying the wrong JSTL taglib URL. This one is for JSTL 1.0. After JSTL 1.1 it requires a /jsp in the path. See also the JSTL 1.1 tag library documentation.
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
As to the of the code (and to reply on all those duplicate answers complaining to use ${} instead), the #{} syntax will only work inside JSP when you're targeting a Servlet 2.5 / 2.1 compatible container with a web.xml conforming Servlet 2.5 spec. Tomcat 6.0 is an example of such a container. The #{} will indeed not work in JSP tags on older containers such as Tomcat 5.5 or older.
For clarity and to avoid confusion among starters, better use ${} all the time in JSP tags. Also better use self-documenting variable names.
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String[] names = {"Hello", "you", "are", "using", "jstl", "in", "jsp"};
request.setAttribute("names", names);
%>
<!DOCTYPE html>
<html lang="en">
<head>
<title>JSTL demo</title>
</head>
<body>
<table>
<tr><td>Print</td></tr>
<c:forEach items="${names}" var="name">
<tr><td>${name}</td></tr>
</c:forEach>
</table>
</body>
</html>
See also:
Our JSTL wiki page
Difference between JSP EL, JSF EL and Unified EL
In JSTL 1.2, you don't want to use #{name} in pure JSP, that's only a JSF artifact. Instead, simply use ${name}.
You need to refer items using expression language like ${name}
U r using # instead of $ before name
Let me know if this resolves.
#{name} is not a valid Java variable reference - looks like you are confusing it with JQuery selector.
Anyways try just using items="${name}"
#{name} is should be like ${name}
oh! might be the jars related to JSTL. check thins link for those jars to include in your project
Below is the final code I am using and it is running...
Posting so that someone can use it... Might help me tomorrow ;)
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<% String[] setName = {"Hello", "you", "are", "using", "jstl", "in", "jsp"};
request.setAttribute("getName", setName);
%>
<html>
<body>
<table>
<tr><td>Print</td></tr>
<c:forEach var="itemName" items="#{getName}">
<tr>
<td>${itemName}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
Learning : I was using <%# taglib prefix="c" uri="http://java.sun.com/jstl/core" %> instead of <%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Categories