Extend MasterPage in JSP - java

I have a source code father JSP like that:
<table id="1" >
<tr>
<td>Table1</td>
</tr>
</table>
<div id=".insertCodeJSPInHere"></div>
<table id="2">
<tr>
<td>Table2</td>
</tr>
</table>
I want when i extend father JSP in children JSP then children JSP like that
<table id="1" >
<tr>
<td>Table1</td>
</tr>
</table>
<div id=".insertCodeJSPInHere"><h1>YenDV</h1></div>
<table id="2">
<tr>
<td>Table2</td>
</tr>
</table>
I write in children JSP:
<div id=".insertCodeJSPInHere"><h1>YenDV</h1></div>
<%#include file="fatherJSP.jsp" %>
But it's not work. I don't understand? HELP!

Try
<jsp:include page="fatherJSP.jsp">
The include directive:
<%#include file="something.html" %>
Used for static imports like html pages

Related

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") %>

SpringMVC - Send nested form in jsp

I have a form (addPassForm) which is nested in another form (addStoreFrom) in the JSP page. How can I send the addPassForm form to the controller?
When I try to send the addPassForm form, addStoreFrom form sends instead.
<s:url value="/addStore" var="urlAddStore"/>
<form:form id="addStoreFrom" modelAttribute="newStore" action="${urlAddStore}" method="POST">
<table border="1">
<tbody>
<tr>
<td><form:label path="title">Title*</form:label></td>
<td><form:input path="title"/></td>
</tr>
...
<tr>
<s:url value="/addPassForm" var="addPassForm"/>
<form:form id="addPassForm" action="${addPassForm}" method="post">
...
<td>
<input type="submit" value="Add"/>
</td>
</form:form>
</tr>
<tr>
<td><input type="submit" value="Save"/></td>
<td/>
</tr>
</tbody>
</table>
</form:form>
It is just because nested forms in not a valid HTML pattern. The browser simply ignore the inner <form></form> tags and sees only one form. Reference : Is it valid to have a html form inside another html form?
It is not a JSP problem (nor a Java one !), but only a incorrect HTML problem. You must use successive forms instead of nested forms (or user javascript as other suggested)
Example with successive forms :
<s:url value="/addStore" var="urlAddStore"/>
<table border="1">
<tbody>
<form:form id="addStoreFrom" modelAttribute="newStore" action="${urlAddStore}" method="POST">
<tr>
<td><form:label path="title">Title*</form:label></td>
<td><form:input path="title"/></td>
</tr>
...
<tr>
<td><input type="submit" value="Save"/></td>
<td/>
</tr>
</form:form>
<tr>
<s:url value="/addPassForm" var="addPassForm"/>
<form:form id="addPassForm" action="${addPassForm}" method="post">
...
<td>
<input type="submit" value="Add"/>
</td>
</form:form>
</tr>
</tbody>
</table>
Explicitly call submit() function in javascript.Add a function and bind it to onclick of add button.<input type="button" value="Add" onclick="submitAddPassForm()"/>.In javasrcipt simply use:
function submitAddPassForm(){
$('#addPassForm').submit();
}
You can just make a ajax post call instead of a form submit

jsp:param don't display the expected value

In my spring project, I have two layers for my views:
1) each method from my controller map directly a jsp page like that:
<jsp:include page="../../common/listagem.jsp">
<jsp:param name="name" value="Usuario"/>
<jsp:param name="elements" value="{['login','senha','pnome','unome','email']}"/>
</jsp:include>
2) my jsp page common/listagem.jsp is this:
<%# include file="../include/header.jsp" %>
<sec:authorize access="hasPermission(#user, 'cadastra_${param.name}')">
<p>
<button type="button" class="btn btn-sm btn-link link" data-action="/${param.name}/cadastra">
Cadastrar novo ${param.name}
</button>
</p>
</sec:authorize>
<table class="bordered">
<thead>
<tr>
<c:forEach var="item" items="${param.elements}">
<th> <c:out value="${item}"/> </th>
</c:forEach>
</tr>
</thead>
<tbody class="content">
</tbody>
<tfoot>
<tr>
<sec:authorize access="hasPermission(#user, 'altera_${param.name}')">
<td class="comando" data-nome="Altera" data-action="/${param.name}/altera"></td>
</sec:authorize>
<sec:authorize access="hasPermission(#user, 'remove_${param.name}')">
<td class="comando" data-nome="Remove" data-action="/${param.name}/remove"></td>
</sec:authorize>
</tr>
</tfoot>
</table>
<c:url value="/${param.name}/listagem.json" var="listagem"/>
<script>
$(document).ready(function(){
load_content("${listagem}", $('table.bordered'));
});
</script>
<%# include file="../include/footer.jsp" %>
My problem it's exactly with the page above. the final result for this code is that:
but instead should display only the title of the columns in the top and the code:
<c:url value="/${param.name}/listagem.json" var="listagem"/>
<script>
$(document).ready(function(){
load_content("${listagem}", $('table.bordered'));
});
</script>
should fill the table with data from the server (this code works in other project mine, where I don't use include files and jsp:param).
Anyone can see what's wrong with this code?
You can try with JSP JSTL Functions but it more like a JSON string so try with Jackson JSON Parser.
Find tutorial on Jackson parser here.
Sample code:
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<c:set var="elements" value="${param.elements}" />
<c:forEach var="item"
items="${fn:split(fn:substring(elements,2,fn:length(elements)-2),',')}">
<c:out value="${item}" />
</c:forEach>
output:
'login' 'senha' 'pnome' 'unome' 'email'
OK, I solve this issue using an array for store the values, like that:
<jsp:param name="elements" value="login"/>
<jsp:param name="elements" value="pnome"/>
<jsp:param name="elements" value="unome"/>
<jsp:param name="elements" value="email"/>
and in the common jsp page:
<c:forEach var="item" items="${paramValues.elements}" varStatus="status">
<th class="col" data-property="<c:out value="${item}"/>">
<c:out value="${paramValues.label[status.index]}"/>
</th>
</c:forEach>

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

JSP resourceBundle

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.

Categories