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
Related
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("'", "\\"));
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>
I wrote a servlet.
request.setAttribute("itemCount", 1000);
request.getRequestDispatcher("test.jsp").forward(request, response);
And in test.jsp I wrote:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String name=(String)session.getAttribute("itemCount");
out.print("Total items are: "+name);
%>
<p>Number of items: ${itemCount} </p>
</body>
</html>
But the second row is
Number of items: 1000
while the first row is
Total items are null
So what's the issue?Any help? Thx.
In your servlet, you can try using:
request.getSession().setAttribute("itemCount", 1000);
& then in your jsp, you can access itemCount the way you are accessing.
Or alternatively,
use
<%
String name=(String)request.getAttribute("itemCount");
in your jsp if you don't want to change any code in servlet (also mentioned by tgdavies in comment)
I have two jsp pages. I am trying to add "Russian" language. Russian characters are shown perfectly on jsp page, but when I try to send this value to another jsp page from parameter then in second jsp page this value is changed to different characters. This problem is only in Russian Language and not in others such as Italy and French.
For example
On demo.jsp page the russian character "приветствие" is shown correctly.
but when I try to send it to another page "test.jsp" then some unknown
characters are shown like "!C<Cä5 Cô>CôCC´OD=Cä5!"
Code:
demo.jsp
String welcometext=langP.get("welcome");
<jsp:include page="<%=test.jsp%>">
<jsp:param name="wlc" value="<%=Encode.hex8Code(welcometext)%>" />
</jsp:include>
In test.jsp
String title = Encode.utfToUnicode(Decode.hex8Decode(request.getParameter("wlc")));
System.out.println(" Russial welcome test "+welcome);
Is there any special code we need to add for Russia while sending them in query parameters??
Please note* the following code are already written else it would have given problem for French and Italy language too..
<%# page contentType="text/html; charset=UTF-8" %>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
Also tried with following but didn't help out!
request.setCharacterEncoding("UTF-8")
Try to add <% request.setCharacterEncoding("UTF-8"); %> to your main jsp page:
Here is my example:
demo.jsp
<%#page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Привет</h1>
<jsp:include page="/test.jsp" flush="true">
<jsp:param name="wlc" value="Привет"/>
</jsp:include>
</body>
</html>
test.jsp
<h1>Param values is</h1>
<%
String hello = request.getParameter("wlc");
out.print(hello);
%>
I don't know the better solution but the following code solved this issue. I kept the variable in session attribute.
demo.jsp
session.setAttribute("welcometext", welcometext);
test.jsp
String welcometest=(String) session.getAttribute("welcometext");
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.
I am developing a simple application on Java EE platform using database as MYSQL and JFreeChart to generate 3D Bar Charts but 3D effect is visible but the bars are not seen on that background,thus I am unable to display the bars in bar chart and same is with when I try to do this with Pie Charts.
Here is mine code:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# page import="java.io.*" %>
<%# page import="java.sql.*" %>
<%# page import="org.jfree.data.jdbc.JDBCCategoryDataset" %>
<%# page import="org.jfree.chart.plot.PlotOrientation" %>
<%# page import="org.jfree.chart.JFreeChart" %>
<%# page import="org.jfree.chart.ChartUtilities" %>
<%# page import="org.jfree.chart.ChartFactory" %>
<%
String connectionURL = "jdbc:mysql://localhost/tester?user=root&password=root&useUnicode=true&characterEncoding=utf-8";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection (connectionURL);
String query = "SELECT * from charter";
JDBCCategoryDataset dataset = new JDBCCategoryDataset(con);
dataset.executeQuery(query);
JFreeChart chart = ChartFactory.createBarChart3D("Test", "Name", "ID",dataset, PlotOrientation.VERTICAL, true, true, false);
try {
ChartUtilities.saveChartAsJPEG(new File("E:/project/jfreechart3D/img/barchart_3D.jpeg"),chart,400, 300);
}
catch (IOException e) {
System.out.println("No chart creation.");
}
%>
<!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>
<IMG SRC="E:/project/jfreechart3D/img/barchart_3D.jpeg" WIDTH="600" HEIGHT="400" BORDER="0" USEMAP="#chart" />
</body>
</html>
Instead, write a servlet that invokes one of the ChartUtilities methods such as writeChartAsJPEG() or writeChartAsPNG(). The former will be more compressed, but the latter will be sharper. There's an example here. Have your JSP include a tag where the src refers to your servlet.