JSF, HighCharts and JS - java

I would like to use in my project highcharts and jsf both. I have a managedbean, which has a list and it is initializing by a facade (it do a query in my database). If I'd like to pass the value of the list to highcharts, how can do this?
if anyone has a good idea, please share.
Best regards and thanks a lot

Haven't worked with highcharts , but I did with other charting library...
I'll give you general Instructions on how to combine js based charting directory it with JSF (I'm sure that there are others way's like using servlets and etc...)
1) build a working "hard coded" js only example in your JSF project
include the relevant *.js files needed by the library
add the "container" div that is required to your page
and finally wrtie the js script that build your chart with hard coded values
2) place your hard coded values into your Bean String property and place a ref' to that property in your .xhtml page something like that <h:inputHidden id="chart_input_data" value="#{myBean.valueOfChart}" /> and access it in your js code like that
//I used jQuery selectors....
var data_for_chart = $('input[id$="chart_input_data"]').val(); //you can use a simpler selector like $("#chart_input_data") too
than use the variable data_for_chart as chart series input (or for whatever parameter of your chart constructor)
3) finally I guess you would like to turn some list of Pojos into a proper json format which is most like wold "fit like a glove" for the HigthCharts constractor , this you can achieve with Gson library something like gson.toJson(yourListOfValues) see Gson user guide
Note
This technique should work for all charting library's , such as flot , flotr2 , gRaphael , jqPlot and more...

Related

Can a JSP template be used from within Java?

I'm pretty new to JSP. So far it seems that the flow of processing is very much Java runs first, then populates a JSP template.
I am wondering if there is a way from within Java to utilize a JSP template. What I mean is, imagine I had a simple "SimpleDiv.jsp" template on classpath like this:
<div id="${id}" class="${class}">
${content}
</div>
And then from within an arbitrary Java file (perhaps not even running on a servlet), I could do something like this:
private String getDivHtml( id, html ) {
Template simpleDiv = TemplateLoader.load("SimpleDiv.jsp");
simpleDiv.set("id", id);
simpleDiv.set("class", Whatever.CLASS_NAME);
simpleDiv.set("content", html);
return simpleDiv.toString();
}
This is a pretty simplistic example so don't get caught up on the details of that. Main question is -- can I pull in a JSP template in Java and cause it to generate HTML inline?
Freemarker and Velocity are very popular for generating content from templates, you might try one of them. Since JSPs are implemented as servlets (and the JSP spec defines them as webcomponents) they are tied to the servlet container.
There's no simple way to accomplish this using plain JSP. There are related Q/As in the site explaining how to do it:
What is the best way to create JSP layout template?
JSP tricks to make templating easier?
Another option using plain JSP would be using external frameworks to accomplish the task like Apache Tiles and SiteMesh (mentioned here: JSP template implementation (Composite View Pattern)).
If you can, upgrade to Facelets, the current view technology since Java EE 6. This technology already provides built-in template system as explained here and here.
Consider this subset of JSP which has no dependency on servlet:
http://jstp.sourceforge.net/manual.html
Jsp is a inner servelt, a java class, normally, it outputs HTML when it finishes executing.
Maybe you should just treat the jsp file as a pure string, the "simpeDiv.set" method just do replacing works: replace ${key} to ${value}

spring mvc - easiest way to check on what page you are and change style of the menu items

I'm working on a spring mvc project. I need to change the style of my menu items when I'm on a particular page. The css is done. But i still need a way to check on which page I am.
What is the easiest way to do this. All solutions are appreciated.
,
thank you
You can integrate Apache Tiles into your Spring MVC project (exmple here) and pass path to css needed in tiles.xml.
Alternatively you can send this path to your JSP page in JavaBean, but it is less declarative and requires accurate manipulating of beans.
Try this short cut.
Set the style's class name in the ModelAndView as a variable. In the JSP files directly use the variable as the style's class name.
In the controller
modelAndView.add("styleVariableName","styleToBeApplied")
In the JSP
<div class="${styleVariableName}">
styleToBeApplied should be a css class and you can repeat this for every controller action.

What are all the changes we have to do in JqGrid, incase of using DWR & java instead of AJAX & PHP?

First, i have to declare myself that, i dont know PHP & AJAX. I know something in DWR, javaScript & java, like i am able to create a Web based CRUD by using them. I want to integrate DWR & JAVA with the jQGrid. I did a lot of research for that. I am not able to find anything that uses JAVA & DWR in jqGrid.
Any conceptual idea or solution will be appreciative.
Any online links will be more appreciative.
Thanks in advance.
You asked about conceptual idea of a possible solution. I try shortly describe a possible way. How I could understend from your previous question you are a beginner in JavaScript and jQuery. So I try to write simple and clear describe the arcitectur of the solution.
Your Web application can consist of pure HTML or XHTML pages (without any JSP pages) with Javascript files loaded and started throght <script type="text/javascript" src="..."></script>. You place HTML/XHTML markup in your *.htm files and the definition of jqGrid in *.js files.
jqGrid has three important parameter mtype which are typically "GET" or "POST", datatype withe the values like "xml" or "json" and url parameter. This three parameters defins how the grid will be filled. There are also editurl cwich are used for CRUD operations. So you can implement a servlet in Java (see How to learn AJAX using jQuery in a Java web app for example) which could be the only active component in your solution which bound to the URL defined by url and editurl and support HTTP GET or POST depend of your mtype choice. It will work like a web service which provide the data for the jqGrids and implement all CRUD operations.
jqGrid will send to servlet some standard parameters. The names of this input parameters of the servlet you can change with prmNames parameter of jqGrid (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options). The most important parameters of the servlet method are following int page, int rows, string sidx, string sord. Adfditional parameters could be also bool _search, string searchField, string searchOper, string searchString if you want to use single searching in th jqGrid or bool _search, string filters in case of advanced searching. So it user click on the "next page" jqGrid button or click on the grids column header to sort the data, your servlet will by called by jqGrid with the corresponding values of the imput parameters.
In the way you have clear structure of your solution. I am not sure that you will need DWR. Just try to find how you can use jQuery.ajax to call your Java servlets. If you will have some problems with implemented the same technique inside of jqGrid you can customize jQuery.ajax requests which will be send by jqGrid with ajaxGridOptions jqGrid parameter. You can also use serializeGridData event of jqGrid (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:events#list_of_events) to implement any data conevrsion before the data will be snd to the servlet and use use jsonReader or xmlReader (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data) which define how the data returned from the server should be readed by jqGrid.

How to JSP display multiple page with one layout (Example index.jsp?page=about display about.jsp)

I new in JSP, i have a problem with JSP
in php i use
$page=$_GET["page"]
for display multiple page for one layout it mean i have index , it display layout and when i click on menu go to about us the index url = index.jsp?page=about
in PHP when i declare $page above and next step i do
Switch($page){
case 1:about
include 'aboutus.php'
case 2:news
include 'news.php'
}
How can i do it ?
How jsp can do the same way php to display multiple page in 1 layout
Use jsp:include.
<jsp:include page="/WEB-INF/${param.page}.jsp" />
And pass ?page=news or ?page=about, etc as parameter. The ${param.page} prints the outcome of request.getParameter("page"). You can prevent direct access to JSP files (by entering URL in browser address bar) by placing JSP files in /WEB-INF folder.
See also:
Basic JSP/Servlet tutorials
Hidden features of JSP/Servlet
How to avoid Java code in JSP
nowadays you use "templates" of Java Server Faces (JSF) for this approach. When you use JSP, you actually don't use the same concept as in PHP. You'd better use the MVC concept. But to answer your question, you could probably achieve this with the include tag http://java.sun.com/products/jsp/tags/11/syntaxref1112.html and control it with JSTL:
http://www.java2s.com/Code/Java/JSTL/JSTLiftag.htm

display tags jsp tags

i am using diaply tags library for displaying my tables.But i cant use the <% %>
tagst there.If i try to us it it gives me error.but i can use tag there.
If i try to use followin code in my jsp it give an error sayin shoul hav a matcheing ending tag.
i have follown java code in jsp
List<Course> = (List<Course>)request.getAattribute("crc");
here Course is a class/bean.
can anyone suggest me such library that i can use with struts for auto paging,displaying list in tables,and with other features provided by display tag.I want to use struts and i want the view to look good and yet easy to devlop.that is i want to achieve high class userinterface with littel effortr toward displaying o/p / view.
can anyone provide the example of disploay tag with struts
You can use the name attribute of the table tag (normally like this:)
<display:table name="crc" ...>
</display:table>
To use the crc List as the basis of Javabeans to display.
See http://displaytag.sourceforge.net/1.2/displaytag/tagreference.html#display-el:table for more information about the table tag.

Categories

Resources