JSP resourceBundle - java

I have no problem loading and using properties file from JSP files which are located in the root of website (using ResourceBundle class) but when I try to load the same properties file from a JSP which is located in a directory it fails and says the resource can not be found!
Code of the page which is located in a directory
<%#page import="org.apache.log4j.Logger"%>
<%#page import="com.persiangulfcup.utility.LogUtils"%>
<%#page import="java.util.ResourceBundle"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
Logger logger = LogUtils.getLogger("page/contact");
ResourceBundle lbls = null;
ResourceBundle msgs = null;
try {
lbls = ResourceBundle.getBundle("labels");
msgs = ResourceBundle.getBundle("messages");
} catch (Exception ex) {
logger.fatal(ex);
}
%>
<div class="form">
<div style="text-align: left; font: normal bold 14px arial; cursor: pointer" onclick="contactBox.hide();">X</div>
<div style="padding-bottom: 10px;font-size: 14px; text-align: center"><%=msgs.getString("contactHeader")%></div>
<form id="frmContact" onsubmit="try {sendContact();} catch (e) {console.log(e)} return false;">
<table class="form">
<tr>
<td class="caption"><%=lbls.getString("name")%>: </td>
<td class="data">
<input id="txtName" type="text" name="txtName"/>
</td>
</tr>
<tr>
<td class="caption"><%=lbls.getString("email")%>: </td>
<td class="data">
<input id="txtEmail" type="text" name="txtEmail"/>
</td>
</tr>
<tr>
<td class="caption"><%=lbls.getString("subject")%>: </td>
<td class="data">
<input id="txtSubject" type="text" name="txtSubject"/>
</td>
</tr>
<tr>
<td class="caption"><%=lbls.getString("message")%>: </td>
<td class="data">
<textarea id="txtMessage" name="txtMessage"></textarea>
</td>
</tr>
<tr>
<td class="button" colspan="2"><input type="submit" value="<%=lbls.getString("send")%>"/></td>
</tr>
<tr>
<td style="text-align: center" colspan="2" id="brdContact"></td>
</tr>
</table>
</form>
</div>

This is because you didn't respect a golden rule: don't put anything in the default package.
A resource bundle is loaded as a class, from the classpath. It has a fully qualified name, which must be used to load it. And it's not possible to use a class in the default package from a class not in the default package.
So, put your resource bundle in an appropriate package (example : com.persiangulfcup.foo.bar), and load them like this : ResourceBundle.getBundle("com.persiangulfcup.foo.bar.labels").
That said, using scriptlets inside JSPs is a bad practice. You should really use the JSTL, which has a fmt library allowing to use resource bundles, format messages, etc.

Related

How to generate a download request from html page to controller of spring mvc

<html>
<head>
<title>Student Registration</title>
</head>
<body>
<h3 align="center">New Trainee Registration</h3>
<table align="center" cellpadding="10">
<!-- First Name -->
<tr>
<td>First Name</td>
<td>${traineeData.fName}</td>
</tr>
<!-- Last Name -->
<tr>
<td>Last Name</td>
<td>${traineeData.lName}</td>
</tr>
<!-- Father's Name -->
<tr>
<td>Father's Name</td>
<td>${traineeData.fatherName}</td>
</tr>
<!-- Mobile Number -->
<tr>
<td>Mobile No.</td>
<td>${traineeData.mobile}</td>
</tr>
<tr>
<td>Joining Date</td>
<td>${traineeData.joiningDate}></td>
</tr>
<tr>
<td>Grade</td>
<td>${traineeData.grade}</td>
</tr>
<tr>
<td>Gender</td>
<td>${traineeData.gender}</td>
</tr>
<!-- Course -->
<tr>
<td>COURSES<br />APPLIED FOR
</td>
<td>${traineeData.course}</td>
</tr>
<tr>
<td>Joining Date</td>
<td>${traineeData.joiningDate}</td>
</tr>
<!-- Submit and Reset -->
<tr>
<td colspan="2" align="center">
<button type="button" >Generate Certificate</button>
</td>
</tr>
</table>
</body>
</html>
Let's say, I have a download button on my HTML page. when clicking on this button a request goes through the controller of the spring MVC framework along with "enrollmentNo".
On Controller class, I will use this enrollmentNo.
I have a class which will generate a certificate for the student by fetching data using the student's enrollmentNo.
and response goes back and a file will download(which a pdf file generated by my pre-created class)
I have tried with many ways but not get what I want.
#RequestMapping(value = "/generateCertificate")
public void generateCertificate(#RequestParam("traineeData.enrollmentNo") int enrollmentNo) {
System.out.println("you in /generateCertificate");
// I don't know what should I return...
// please change return type accordingly...
}
I want a button on my HTML page named "Generate certificate". The code should be easy and normal, Ajax can be used here.
Your button isn't actually doing anything, useful.
Try this code around your button...
<tr>
<td colspan="2" align="center">
<form action="/generateCertificate" method="GET">
<input type="hidden" name="enrollmentNo" value="${traineeData.enrollmentNo}"
<button type="submit">Generate Certificate</button>
</form>
</td>
</tr>
Then your controller will need to changed slightly...
#RequestMapping(value = "/generateCertificate")
public void generateCertificate(#RequestParam("enrollmentNo") int enrollmentNo) {
// The #RequestParam was changed to the name of the param from the form and not the object/value.
}

JasperException when using f: in jsp page

I'm trying t learn Spring MVC and my project is buid a jsp form input a product
I added all jar jstl lib, and here is my taglib
<%#taglib uri="http://www.springframework.org/tags/form" prefix="f"%>
<%#taglib uri="http://www.springframework.org/tags" prefix="s"%>
Here is my form appears bug
`<f:form action ="insertProduct.htm" method="GET" commandName="newProduct">
<table border ="1" cellpadding="2" cellspacing="2">
<tr>
<td>Product ID</td>
<td><f:input path="productId"/></td>
</tr>
<tr>
<td>Product Name</td>
<td><f:input path="productName"/></td>
</tr>
<tr>
<td>Price</td>
<td><f:input path="price"/></td>
</tr>
<tr>
<td>Description</td>
<td><f:input path="description"/><td>
</tr>
<tr>
<td>Status</td>
<td>
<f:select path="status">
<f:option value="true" label="Active"/>
<f:option value="false" label="Inactive"/>
</f:select>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Insert"/>
<input type="reset" value="Reset"/>
<input type="button" value="Back" onclick="history.go(-1)"/>
</td>
</tr>
</table>
</f:form>`
But i cant fix my bug. Please help me :(

Get data from jsp to another

hello everyone i've been looking how get data from a list from a jsp and put it into another jsp with a form but i dont know how can i get the id from the list and send it to my form :/ btw im working on netbeans
this is my list.jsp that shows on screen all the errors registered on db
<h1>List of Errors</h1>
<%
ErrorDAO daoaut=new ErrorDAO();
List<ErrorENT> list=daoaut.list();
%>
<table BORDER="3">
<tr>
<td>ID</td><td>DATE</td><td>LOG</td><td>ESTATE</td><td> </td>
</tr>
<% for(ErrorENT aut:list){ %>
<tr>
<td> <%= aut.getIdError()%> </td>
<td> <%= aut.getDate()%> </td>
<td> <%= aut.getLog() %> </td>
<td> <%= aut.isEstate() %> </td>
<td><input type="button" id="btnUpdate" value="Update"/></td>
</tr>
<% } %>
</table>
and somehow when i click on the button Update it should get the id from the row and pass it to my form, but i dont know how to get the id and how it should be recived from the other side :(
Using Hidden Tag Or using anchor tag
<h1>List of Errors</h1>
<%
ErrorDAO daoaut=new ErrorDAO();
List<ErrorENT> list=daoaut.list();
%>
<table BORDER="3">
<tr>
<td>ID</td><td>DATE</td><td>LOG</td><td>ESTATE</td><td> </td>
</tr>
<% for(ErrorENT aut:list){ %>
<form action="Your another jsp" method="post"> // using form for submit to another jsp
<tr>
<td> <%= aut.getIdError()%> </td>
<td> <%= aut.getDate()%> </td>
<td> <%= aut.getLog() %> </td>
<td> <%= aut.isEstate() %> </td>
<td><input type="hidden" value="<%=aut.getIdError()%>" name="id"/></td>
<td><input type="button" id="btnUpdate" value="Update"/></td>
</tr>
</form>
<% } %>
</table>
// now you can IdError from next page using request.getParameter("id");
2 ND METHOD USING anchor tag
<%
ErrorDAO daoaut=new ErrorDAO();
List<ErrorENT> list=daoaut.list();
%>
<table BORDER="3">
<tr>
<td>ID</td><td>DATE</td><td>LOG</td><td>ESTATE</td><td> </td>
</tr>
<% for(ErrorENT aut:list){ %>
<tr>
<td> <%= aut.getIdError()%> </td>
<td> <%= aut.getDate()%> </td>
<td> <%= aut.getLog() %> </td>
<td> <%= aut.isEstate() %> </td>
<td> <input type="button" id="btnUpdate" value="Update"/> </td>
</tr>
</form>
<% } %>
</table>
Actually I prefer to use Servlets or some MVC framework (SpringMVC, Struts2) instead.
However if you want to achieve your target with just using JSPs:
In list.jsp :
//use this :
<td><a href="/otherJsp.jsp?idError=<%= aut.getIdError() %>" > Update </a></td>
//instead of :
<td><input type="button" id="btnUpdate" value="Update"/></td>
In the otherJsp.jsp
//Retrieve the Error ID from request
The Error ID is : <%= request.getParameter("idError") %>

Starting tag <td> not closed?

Please have a look at the below code:
<table border="0" width="100%" id="id1">
<tr>
<td width="15%" nowrap>
<Label class="someClass"> Testing </Label>
</td>
//Error complaining at below line
<td width="30%" class="someClass" nowrap>
<html-el:text styleClass="Test" property="someProperty" size="30" maxlength="10"/>
</td>
</tr>
</table>
For the first td I've closed it but at the second td it's complaining IWAK0061E Start tag() not closed.
May I know what's wrong?
I don't know why you use such weird html syntax ,however I would use:
<table border="0" width="100%" id="id1">
<tr>
<td width="15%">
<Label class="some class"> Testing </Label>
</td>
<td width="30%" class="some class">
<textarea property="some property" maxlength="10" class="some class"></textarea> // all visual stuff is used with class tag
<input property="some property" maxlength="10" class="some class"> // or input with one line field
</td>
</tr>
</table>
Also, nowrap attribute is not supported in HTML5. Use CSS instead.

Linking HTML to JSP page

I'm have problems linking my HTML and JSP. Just trying to make a simple html page to take a "email" and "password" then have the jsp display on the next page. But when i click submit the parameters dont get passed i just get "null", any ideas where im going wrong?
HTML Page
<html>
<head>
<title>Enter your email and password</title>
</head>
<body>
<form action="form.jsp" method="get">
<table cellspacing="5" border="0">
<tr>
<td align="right">Email:</td>
<td><input type="text" email=email></td>
</tr>
<tr>
<td align="right">Password:</td>
<td><input type="text" password=password></td>
</tr>
<tr>
<td></td>
<td><br><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
JSP Page
<html>
<body>
<%
// get info from request
String email = request.getParameter("email");
String password = request.getParameter("password");
%>
<p>Here is the info you provided:</p>
<table cellspacing="5" cellpadding="5" border="1">
<tr>
<td align="right">Email:</td>
<td><%= email %></td>
</tr>
<tr>
<td align="right">Password:</td>
<td><%= password %></td>
</tr>
</table>
<form action= "Next" method="post">
<input type="submit" value="Return">
</form>
</body>
</html>
You need to use the name attribute in the form fields .
<input type="text" name="email"></td>
The value of "email" can be retrieved in JSP as :
String email = request.getParameter("email");
OR
${param.email}
OR
<c:out value="${param.email}"/> <%--(Using JSTL)--%>
Here are the complete list of attributes.
name = string #
The name part of the name/value pair associated with this element for the purposes of form submission.
You should specify the input name attribute. Try this,
<form action="form.jsp" method="get">
<table cellspacing="5" border="0">
<tr>
<td align="right">Email:</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td align="right">Password:</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td></td>
<td><br><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
For more info you can try this link

Categories