I'm trying to call a method that connects that is on a Bean on a jsp file. The method will make a request to a RMI Server and return a string. At this point the method is just return a pre-defined string for test.
This is the bean method:
public String getListProjects() throws RemoteException {
this.dataToSend = new Object[2];
this.dataToSend[1] = 0;
this.postCard = new ClientRequest("2", this.dataToSend, "tempo");
try{
this.postCard = this.connectToRMI.getActualProjects(this.postCard);
}catch(Exception e){
e.printStackTrace();
}
return "Hello";
}
And this is the jsp code:
<h1>Projectos Actuais</h1>
<h2><%
fundstarter.model.ConnectToRMIBean aux = new fundstarter.model.ConnectToRMIBean();
try{
aux.getListProjects();
}catch(Exception e){
e.printStackTrace();
}
%>
</h2>
I'm guiding my self from another code, and the method is called like this. But in my case it's not working, and I can't figure out what is wrong.
Since you've tagged this struts2, assuming the getListProjects() is on the Action, in JSP use:
<s:property value="listProjects" />
If instead it is on a bean, declare the bean in the Action, and expose it through a getter:
private MyBean bean;
public MyBean getBean(){
return bean;
}
and in JSP use the dot notation:
<s:property value="bean.listProjects" />
P.S: always avoid Scriptlets (<% %>), they're evil.
Quoting and fixing your latest change on edit with some comments:
<h1>Projectos Actuais</h1>
<h2><%
try{
fundstarter.model.ConnectToRMIBean aux = new fundstarter.model.ConnectToRMIBean();
//Send result into generated HTML page with out.print!
out.print(aux.getListProjects());
}catch(Exception e){
e.printStackTrace();
}
%>
</h2>
As per the flow of Struts there should be field in beanclass with same name of getter & setter. For an example if your method name is getListPorjects then in your bean class there should be a private string variable name listprojects.
Also your method will update with following to way to return listprojects.
example:
public String getListProjects() throws RemoteException {
this.dataToSend = new Object[2];
this.dataToSend[1] = 0;
this.postCard = new ClientRequest("2", this.dataToSend, "tempo");
try{
this.postCard = this.connectToRMI.getActualProjects(this.postCard);
listprojects = "hello"
}catch(Exception e){
e.printStackTrace();
}
return listprojects;
}
Calling bean variable should be with ID over the JSP page.
<jsp:useBean id="aux" class="com.path.to.ConnectToRMIBean" scope="request" />
----
yours stuff
-----
<h1>${aux.listProjects}
hope this will help you. good luck
You are just missing the way <% %> and <%= %> are used in JSP. to print in <% %> tags use
<% out.println("Your results"); %>
and for <%= %>
<%=
String.valueOf(1+2);
%>
Related
I have been struggling to print text inside a Java method on a Java server page. Here are some ways I've attempted to print the text and errors thrown from the Tomcat (Version 7.0.56) compiler:
<%
class Base {
public void main() {
String Text = "ThisIsText";
out.println(Text);
}
}
%>
Error: Cannot refer to the non-final local variable out defined in an enclosing
scope
<%
class Base {
static Text;
public void main() {
String Text = "ThisIsText";
}
out.println(Text);
}
%>
Syntax error, insert "Identifier (" to complete MethodHeaderName
<%
class Base {
static Text;
public String text() {
String Text = "NewText";
return Text;
}
}
%>
<%text();%>
The method text() is undefined for the type Base_jsp
Is there any way to print HTML Text directly from a Java method in a JSP scriptlet?
The out variable is already inside a method - _jspService. So if you are going to use out, declare another method that takes out as parameter using declaration - <%! and then just call this method.
Use a declarative tag instead
<%!
class Base {
JspWriter out;
public Base(JspWriter out) {
this.out = out;
}
public void main() {
String Text = "ThisIsText";
try {
out.println(Text);
} catch (Exception e) {
e.printStackTrace();
}
}
}
%>
<%(new Base(out)).main();%>
when you write something in <%%> the code between these tags is placed in methods so you cant declare function or class inside that tags because we cant have methods or class inside methods.
Similar,Also check
I have a page who is divided by campus. Is basically the same page but I change the information depending to the campus.
The param "campus", set who is the currently used campus.
This work very well, for example if I access to localhost/page/campus1/index.action, the getCampus method return "campus1", has expected.
But if I access to localhost/page/campus1/index.action?campus=campus2, the getCampus() method return "campus2"; I try to avoid this behavior. I want the {campus} param was in the top of the priority and nobody can override him.
I tray using "excludeParams" from the "param" interceptor to exclude "campus" but now getCampus return null.
how can I exclude some get/post method from the param read by struts2?
or
can avoid the static param been override by others param?
EDIT:
I have a interceptor who say if the chosen campus is valid one or not.
If someone enter a real campus in the url, but put a "get method" with a wrong campus the test will pass but all the information will be wrong.
My action
#Action(value="{campus}/index",
results={
#Result(name=SUCCESS,location="/jsp/campusindex.jsp"),
}
)
public String campusIndex(){
return SUCCESS;
}
public void setCampus(String campus){
this.campus=campus;
}
public String getCampus(){
return this.campus;
}
My jsp
<%# taglib prefix="s" uri="/struts-tags"%>
<s:label name="campus"/>
My Interceptor
public String intercept(ActionInvocation actionInvocation) throws Exception {
ActionConfig config = actionInvocation.getProxy().getConfig();
String campus = config.getParams().get("campus");
try{
Campus.isaValidCampus(campus);
} catch(Exception e){
return "error404";
}
return actionInvocation.invoke();
}
When I write code in <%, I can use javax.naming, but when I write code in <%!, I get an error that javax.naming does not exist.
some examples:
<% Javax.naming.InitialContext ic = new Javax.naming.InitialContext(); %> fine
<%! void foo() { Javax.naming.InitialContext ic2 = new Javax.naming.InitialContext(); } %> error
The exact error:
rptCountsRecon_jsp.java:22: package Javax.naming does not exist
Javax.naming.Context init = new Javax.naming.InitialContext();
I am sortof new to Java and am very new to JSP.
EDIT: added the foo method to the second example, because it may help understand my issue. Also added the exact error from Tomcat.
EDIT 2: tried adding the prefix to Context, and got a similar error.
EDIT 3: Side question... The current code gives the following error:
try
{
javax.naming.Context init = new javax.naming.InitialContext();
Context ctx = (Context) init.lookup("java:comp/env");
String jndiName = getServletContext().getInitParameter("jndiName");
DataSource ds = (DataSource) ctx.lookup(jndiName);
Connection dbConn = ds.getConnection();
PreparedStatement pstmnt = dbConn.prepareStatement(sql);
for(int i=0; i!=binds.size(); ++i)
pstmnt.setString(i+1, binds.elementAt(i)); //binds index starts at 1 -_-
return pstmnt.executeQuery();
} catch (Exception e) {
//out.println("A naming exception occured... I don't know how to get the info of it.");
return null;
}
rptCountsRecon_jsp.java:28: unreported exception java.sql.SQLException; must be caught or declared to be thrown
Connection dbConn = ds.getConnection();
First of all this type code should be in a servlet, not in a JSP.
Declaration (<%!):
<%!
javax.naming.InitialContext ic2 = new javax.naming.InitialContext();
%>
ic2 is a member variable. And the reason that you can't do ic2 = new javax.naming.InitialContext(); there is because the constructor throws javax.naming.NamingException.
Imagine something as follows:
public class JspServlet {
//there is no way to handle that exception here
javax.naming.InitialContext ic2 = new javax.naming.InitialContext();
//...
Scriptlet (<%):
<%
javax.naming.InitialContext ic = new javax.naming.InitialContext();
%>
And the reason that it can be done there (within scriplet) is because everything in the scriplet goes inside a try block of the generated JSP service method.
Imagine something as follows:
public class JspServlet {
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
try {
javax.naming.InitialContext ic = new javax.naming.InitialContext();
} catch (Exception e) {
//...
}
I think it will make more sense if you read more about JSP lifecycle.
I agree with what Bhesh posted. But, you could do the following.
<%!
public void jspInit(){
try{
javax.naming.InitialContext ic2 = new javax.naming.InitialContext();
} catch (javax.naming.NamingException ne){
// do something when exception occurs
}
}
%>
The JSP container calls the jspInit method when it loads the page.
I'm new to JSP. I tried connecting MySQL and my JSP pages and it works fine. But here is what I needed to do.
I have a table attribute called "balance". Retrieve it and use it to calculate a new value called "amount". (I'm not printing "balance").
<c:forEach var="row" items="${rs.rows}">
ID: ${row.id}<br/>
Passwd: ${row.passwd}<br/>
Amount: <%=Calculate.getAmount(${row.balance})%>
</c:forEach>
It seems it's not possible to insert scriptlets within JSTL tags.
You cannot invoke static methods directly in EL. EL will only invoke instance methods.
As to your failing scriptlet attempt, you cannot mix scriptlets and EL. Use the one or the other. Since scriptlets are discouraged over a decade, you should stick to an EL-only solution.
You have basically 2 options (assuming both balance and Calculate#getAmount() are double).
Just wrap it in an instance method.
public double getAmount() {
return Calculate.getAmount(balance);
}
And use it instead:
Amount: ${row.amount}
Or, declare Calculate#getAmount() as an EL function. First create a /WEB-INF/functions.tld file:
<?xml version="1.0" encoding="UTF-8" ?>
<taglib
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<display-name>Custom Functions</display-name>
<tlib-version>1.0</tlib-version>
<uri>http://example.com/functions</uri>
<function>
<name>calculateAmount</name>
<function-class>com.example.Calculate</function-class>
<function-signature>double getAmount(double)</function-signature>
</function>
</taglib>
And use it as follows:
<%#taglib uri="http://example.com/functions" prefix="f" %>
...
Amount: ${f:calculateAmount(row.balance)}">
Another approach is to use Spring SpEL:
<%#taglib prefix="s" uri="http://www.springframework.org/tags" %>
<s:eval expression="T(org.company.Calculate).getAmount(row.balance)" var="rowBalance" />
Amount: ${rowBalance}
If you skip optional var="rowBalance" then <s:eval> will print the result of the expression to output.
If your Java class is:
package com.test.ejb.util;
public class CommonUtilFunc {
public static String getStatusDesc(String status){
if(status.equals("A")){
return "Active";
}else if(status.equals("I")){
return "Inactive";
}else{
return "Unknown";
}
}
}
Then you can call static method 'getStatusDesc' as below in JSP page.
Use JSTL useBean to get class at top of the JSP page:
<jsp:useBean id="cmnUtilFunc" class="com.test.ejb.util.CommonUtilFunc"/>
Then call function where you required using Expression Language:
<table>
<td>${cmnUtilFunc.getStatusDesc('A')}</td>
</table>
Bean like StaticInterface also can be used
<h:commandButton value="reset settings" action="#{staticinterface.resetSettings}"/>
and bean
package com.example.common;
import com.example.common.Settings;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean(name = "staticinterface")
#ViewScoped
public class StaticInterface {
public StaticInterface() {
}
public void resetSettings() {
Settings.reset();
}
}
EL 2.2 has inbuild mechanism of calling methods. More here: oracle site.
But it has no access to static methods. Though you can stil call it's via object reference. But i use another solution, described in this article: Calling a Static Method From EL
If you're using struts2, you could use
<s:var name='myVar' value="%{#package.prefix.MyClass#myMethod('param')}"/>
and then reference 'myVar' in html or html tag attribute as ${myVar}
Based on #Lukas answer you can use that bean and call method by reflection:
#ManagedBean (name = "staticCaller")
#ApplicationScoped
public class StaticCaller {
private static final Logger LOGGER = Logger.getLogger(StaticCaller.class);
/**
* #param clazz
* #param method
* #return
*/
#SuppressWarnings("unchecked")
public <E> E call(String clazz, String method, Object... objs){
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
final List<Class<?>> clasesparamList = new ArrayList<Class<?>>();
final List<Object> objectParamList = new ArrayList<Object>();
if (objs != null && objs.length > 0){
for (final Object obj : objs){
clasesparamList.add(obj.getClass());
objectParamList.add(obj);
}
}
try {
final Class<?> clase = loader.loadClass(clazz);
final Method met = clase.getMethod(method, clasesparamList.toArray(new Class<?>[clasesparamList.size()]));
return (E) met.invoke(null, objectParamList.toArray());
} catch (ClassNotFoundException e) {
LOGGER.error(e.getMessage(), e);
} catch (InvocationTargetException e) {
LOGGER.error(e.getMessage(), e);
} catch (IllegalAccessException e) {
LOGGER.error(e.getMessage(), e);
} catch (IllegalArgumentException e) {
LOGGER.error(e.getMessage(), e);
} catch (NoSuchMethodException e) {
LOGGER.error(e.getMessage(), e);
} catch (SecurityException e) {
LOGGER.error(e.getMessage(), e);
}
return null;
}
}
xhtml, into a commandbutton for example:
<p:commandButton action="#{staticCaller.call('org.company.Calculate', 'getAmount', row.balance)}" process="#this"/>
<c:forEach var="row" items="${rs.rows}">
ID: ${row.id}<br/>
Passwd: ${row.passwd}<br/>
<c:set var="balance" value="${row.balance}"/>
Amount: <%=Calculate.getAmount(pageContext.getAttribute("balance").toString())%>
</c:forEach>
In this solution, we're assigning value(Through core tag) to a variable and then we're fetching value of that variable in scriplet.
In Struts2 or Webwork2, you can use this:
<s:set name="tourLanguage" value="#foo.bar.TourLanguage#getTour(#name)"/>
Then reference #tourLanguage in your jsp
I have a question on code reuse in JSP. I have a JSP page example.jsp that issues a call to a database and gets the results. I have a java class HelperClass.java that accepts a record and prints out the different fields
response.getWriter().println
Now my JSP page has HTML as well and the problem is the content printed out by the HelperClass appears before the content in the JSP page. E.g.
<body>
This is the first line <br/>
HelperClass.printdata("second line");
</body>
output is
secondline This is the first line
Is this a known issue. What is the best way to design an HelperClass for a JSP page that prints content out to the page. Any pointers would be greatly appreciated.
Just do not use a "HelperClass to print data". This makes no sense. There you have EL for.
${bean.property}
That's all. Use a servlet to control, preprocess and postprocess requests. Use taglibs (e.g. JSTL) and EL to access and display backend data.
Here's a basic kickoff example of a Servlet which preprocesses the request before display in JSP:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Person> persons = personDAO.list(); // Get list of persons from DB.
request.setAttribute("persons", persons); // So it's available as `${persons}` in EL.
request.getRequestDispatcher("/WEB-INF/persons.jsp").forward(request, response); // Forward to JSP for display.
}
Here, the Person is just a Javabean class which represents a real world entity.
public class Person {
private Long id;
private String name;
private String email;
private Integer age;
// Add/generate getters and setters here.
}
The PersonDAO#list() method just returns a List of Person objects from the DB:
public List<Person> list() throws SQLException {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
List<Person> persons = new ArrayList<Person>();
try {
connection = database.getConnection();
statement = connection.createStatement("SELECT id, name, email, age FROM person");
resultSet = statement.executeQuery();
while (resultSet.next()) {
Person person = new Person();
person.setId(resultSet.getLong("id"));
person.setName(resultSet.getString("name"));
person.setEmail(resultSet.getString("email"));
person.setAge(resultSet.getInteger("age"));
persons.add(person);
}
} finally {
if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {}
if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
}
return persons;
}
Map the servlet in web.xml on an url-pattern of /persons. The JSP is hidden in /WEB-INF so that nobody can access it directly without requesting the servlet first (else one would get an empty table).
Now, here's how persons.jsp look like, it uses JSTL (just drop jstl-1.2.jar in /WEB-INF/lib) c:forEach to iterate over a List and it uses EL to access the backend data and bean properties. The servlet has put the List<Person> as request attribute with name persons so that it's available by ${persons} in EL. Each iteration in c:forEach gives a Person instance back, so that you can display their proeprties with EL.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<table>
<c:forEach items="${persons}" var="person">
<tr>
<td>${person.name}</td>
<td>${person.email}</td>
<td>${person.age}</td>
</tr>
</c:forEach>
</table>
Call it by http://example.com/contextname/persons. That's all. No need for a "HelperClass to print data" ;) To learn more about JSTL, check Java EE tutorial part II chapter 7 and to learn more about EL, check Java EE tutorial part II chapter 5. To learn more about stuff behind PersonDAO, check this article.