problem loading values to my extjs grid using json - json

I am loading the values coming from database via a json object using java struts,
but the values are not populating in my extjs grid: I keep getting an empty grid.
I have included my code below.
home.jsp
A button will be there on this page. On clicking the getvalues.jsp should come.
In getvalues.jsp an extgrid should be presen with the content coming from database
<%# 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="getvalues.do">
<input type="submit"></input>
</form>
</body>
</html>
Below is my Java code. I am populating a JSON object with the values from my database.
public class Json extends Action {
#Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
ArrayList<Employee> emp=new ArrayList<Employee>();
Myservice serve=new Myservice();
emp=serve.getemployeesservice();
Iterator<Employee> empitr=emp.iterator();
JSONArray json=new JSONArray();
JSONObject JSONobj=new JSONObject();
while(empitr.hasNext()){
JSONObject jobj=new JSONObject();
Employee empl=new Employee();
empl=empitr.next();
jobj.put("empid",empl.getEmpid());
jobj.put("empname",empl.getEmpname());
json.add(jobj);
}
JSONobj.put("employee",json);
System.out.println(JSONobj.toString());
return mapping.findForward("success");
}
}
getvalues.jsp
<%# 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="css/ext-all.css">
<script type="text/javascript" src="js/ext-base.js"></script>
<script type="text/javascript" src="js/ext-all.js"></script>
<script type="text/javascript">
Ext.onReady(function() {
var store=new Ext.data.JsonStore({
proxy:new Ext.data.HttpProxy({
url:'http://localhost:8080/JsonExample/getvalues.do'
}),
reader:new Ext.data.JsonReader({
root:'employee',
fields:['empid','empname']
})
});
store.load();
var recs=store.getRange();
alert(recs.length);
var grid = new Ext.grid.GridPanel({
title:'employee information',
columns: [{
header:"employeeid",
width:100,
dataIndex:'empid',
sortable:true
},{
header:"employeename",
width:100,
dataIndex:'empname',
sortable:true
}],
store: store,
width:300,
height:300,
renderTo:Ext.getBody()
});
});
</script>
</head>
<body>
hi
</body>
</html>
But the values are not being populated for some reason. Please help me solve this issue.
Thanks in advance!

I'm not good at Java. But, I suspect, your JSON list is not sent to client, you are just printing, but not including it in response.

Related

Thymeleaf + Spring Boot: placing html in subfolders

By default, Thymeleaf needs all the html files to be present in scr/main/java/resources/templates. In order not to create a mess in templates folder I need to create different subloders there. The problem is that html files placed there never get resolved. Example is below.
Structure:
IndexController:
#Controller
public class IndexController {
#GetMapping(value = "/")
public String index() {
return "index";
}
}
default template:
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<meta charset="UTF-8"/>
<title>Default template</title>
</head>
<body>
<section id="site-content" layout:fragment="content"></section>
</body>
</html>
index:
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="default">
<head>
<meta charset="UTF-8"/>
<title>Index</title>
</head>
<body>
<div layout:fragment="content">
<div>
This is index page
</div>
Page
</div>
</body>
</html>
page in the subfolder:
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="default">
<head>
<meta charset="UTF-8">
<title>Page</title>
</head>
<body>
<div layout:fragment="content">
This is a page in the subfolder
</div>
</body>
</html>
When I open index page and click the link to the page.html, I get this:
What I did wrong?
You have no controller mapping for /subfolder/page.html, this is not how Thymeleaf and Spring work.
You can't easily reference plain HTML files, as they aren't technically stored there. You have to reference an endpoint defined within Spring which will then select the HTML file to be rendered, like so:
#Controller
public class IndexController {
#GetMapping(value = "/subfolder/page")
public String index() {
return "subfolder/page";
}
}
and create your link like this:
Page

Issue with web application using JSP and Hibernate

Below is my index.html and action.jsp code
When I am submitting the values from index.html to action.jsp instead of getting the output of out.println in action.jsp I am getting the complete action.jsp as my output.
Advise what is the issue and how to rectify it also advise if the directory structure is correct i.e.; all files are placed where they are usually supposed to be placed or not
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="action.jsp" method="post">
Name <input type="text" name = "name"> <br>
Password <input type="password" name="password"> <br>
<input type="submit" value="submit">
</form>
</body>
</html>
// Below is action.jsp
<%#page import="p1.NewHibernateUtil"%>
<%#page import="org.hibernate.Transaction"%>
<%#page import="p1.User"%>
<%#page import="org.hibernate.Session"%>
<%#page import="org.hibernate.SessionFactory"%>
<%#page import="org.hibernate.cfg.Configuration"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
out.println("In scriplet tag");
Configuration cfg = new Configuration();
out.println("Configuration object created");
cfg.configure("hibernate.cfg.xml");
out.println("Configured");
// SessionFactory sf = NewHibernateUtil.getSessionFactory();
// out.println("SessionFactory created");
// Session ses = sf.openSession();
// Transaction t = ses.beginTransaction();
// String n = request.getParameter("name");
// String p = request.getParameter("password");
//
// out.println("Welcome " + n);
// User u1 = new User(n, p);
// ses.save(u1);
// t.commit();
// ses.close();
// out.println("Data inserted successfully");
%>
</body>
</html>
As per my assumption you are running the given example without using web server. You will require web server to run given code.
Consider using web server (ex. Tomcat http://tomcat.apache.org/tomcat-8.0-doc/index.html )

ASP Classic XMLHTTP GET JSON

I am trying to retrieve the output from the a URL using XMLHTTP GET:
The output in the browser when I hit the url directly is the following:
{
"Titles": {
"resultCount": 37680,
"moreResources": true
}
}
The ASP code on test.asp I am using is:
<%#language=JScript%>
<%
var objSrvHTTP;
objSrvHTTP = Server.CreateObject ("Msxml2.ServerXMLHTTP.6.0");
objSrvHTTP.open ("GET","http://someipaddress:8080/Publisher/Titles/Paging/0,0,tc?output=json", false);
objSrvHTTP.send ();
Response.ContentType = "application/json";
Response.Write (objSrvHTTP.responseText);
%>
The results displayed in browser from hitting test.asp is:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>something</title>
</head>
<body>
{
"Titles": {
"resultCount": 37698,
"moreResources": true
}
}
</body>
</html>
I am looking to have just the data between the body tags returned, or even better just the value for "resultCount". Any help would be much appreciated.
You need to remove the HTML markup, when reading JSON data it should be nothing but valid JSON in the request response.

Displaying img in jsp from mysql

I am trying to insert image in mysql database & retrive it and displayimg it another jsp page but while retriving its not displaying image in browser instead it started download can you help me in this. . , and my code is . .,
Addimage.jsp.
<%# 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="AddImg.jsp" method="post">
Image<input type="file" name="file" value="upload" /><br>
<input type="submit" value="Add"/>
</form>
</body>
</html>`
and AddImg.jsp
<%#page import="java.sql.*" %>
<%#page import="java.io.*" %>
<%
Connection con;
PreparedStatement stmt;
ResultSet rs;
byte[] b=null;
//int f;
%>
<%# 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">
<%
String filenm=request.getParameter("file");
String f="C:/visualverify02/limitedimages/"+filenm;
File file=new File(f);
FileInputStream fin =new FileInputStream(file);
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/imgdb","root","password");
stmt=con.prepareStatement("insert into img values(?)");
//String str="insert into img values ('"+file2+"')";
stmt.setBinaryStream(1,(InputStream) fin, (int) (file.length()));
int count=stmt.executeUpdate();
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
if(count>0)
{
out.println("img inserted");
rs = stmt.executeQuery("SELECT * FROM img ");
int i = 1;
if(rs.next()) {
Blob len1 = rs.getBlob("imgfile");
//int len = (int)len1.length();
b = len1.getBytes(1,(int)len1.length());
}else{out.println("Img not selected");}
response.setContentType("image/jpg");
OutputStream o=response.getOutputStream();
%>
<table>
<tr><td><%o.write(b); %></td></tr>
<%
o.flush();
o.close();
}
else{
out.println("Not inserted");
}
%>
</table>
</body>
</html>
First of all I think it's bad practice to have scriptlets in your jsp. I would suggest use at least a http servlet as a controller that deals with storing the image.
Secondly, the way the code is written you are not writing an image to a html page, but to a file, that will trigger downloading that file. To do what you want to archive your contenttype should be "text/html" and the output should be a html with an image tag like described in how to show byte data in an img tag

How to insert content in a keywords metatag with JSP

I have the following JSP page:
<%#page import="com.myPath.JSPHelper"%>
<%#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">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="${jspHelper.getKeywordsMetatag()}">
</head>
<body>
<%
JSPHelper jspHelper = new JSPHelper();
jspHelper.loadData(request.getAttribute("id").toString()); // load data from database
%>
<script type="text/javascript">
<%=jspHelper.getScriptContent()%>
</script>
</body>
</html>
What I'm trying to do is to fill in the contents of the keywords meta tag using a function getKeywordsMetatag() that is defined in a companion class `JSPHelper.java'.
But this is not working, I get the following error:
The function getKeywordsMetatag must be used with a prefix when a default namespace is not specified
I'm new to JSP so I've tried many things without success.
What am I doing wrong here?
What really bothers me is that the function getScriptContent() perfectly works, dumping javascript code in the html page. Why does getScriptContent() work but not getKeywordsMetatag()?
Thanks!
I would move the declaration of jspHelper up above its first use. I would also drop the ${} syntax but that might not be necessary.
<%#page import="com.myPath.JSPHelper"%>
<%#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">
<%
JSPHelper jspHelper = new JSPHelper();
%>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="<%=jspHelper.getKeywordsMetatag()%>">
</head>
<body>
<%
jspHelper.loadData(request.getAttribute("id").toString()); // load data from database
%>
<script type="text/javascript">
<%=jspHelper.getScriptContent()%>
</script>
</body>
</html>