I have a class with a property called title and I have a getter/setter that gets and sets the property. If the property is P I need to print the word "Peer" on the page, and if it's T I need to print "Team" on the page. Can I do this in a JSP without using a scriplets? I tried using
<jsp:getProperty name="value" class"classname" />
but from there I have no idea how to use a conditional in a JSP. Please help.
Use JSTL, as #CoolBeans says. It would look something like this:
In the servlet,
// where myBean is an instance of the class with [get|set]Title
request.setAttribute("myFoo", myBean);
Then, in the JSP,
<c:choose>
<c:when test="${myBean.title eq 'P'}">Peer</c:when>
<c:when test="${myBean.title eq 'T'}">Team</c:when>
</c:choose>
If you're not familiar with JSTL, I'd recommend reading through the JSP section of the Java EE 5 Tutorial, or picking up a copy of Head First Servlets and JSP (it's quite good).
You should use JSTL. Here is an example:
<%# taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<c:if test="${yourClass.p eq 'P'}">PEER</c:if>
I used properties to set and get data from the user, by creating a java class handler and use a "useBean"... Here is some code I generated to fix my problem... hope it helps..
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login and Do Action</title>
</head>
<% // java code
%>
<body>
<jsp:useBean id="myBean" scope="session" class="org.mypackage.IFPWAFCAD.NameHandler" />
<jsp:setProperty name="myBean" property="name"/>
<jsp:setProperty name="myBean" property="screenName"/>
<jsp:setProperty name="myBean" property="username"/>
<jsp:setProperty name="myBean" property="password" />
<h1>Hello <jsp:getProperty name="myBean" property="screenName" />! Ready to Login and perform Action</h1>
<form method="post" action="DBConnection">
<table border="0">
<thead>
<tr>
<th>
</th>
<th>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>Table:</td>
<td><input type="radio" name="tableName" value="product"/>Produce
<<input type="radio" name="tableName" value="customer" />Customer
<input type="radio" name="tableName" value="actor" checked="checked" />Actor</td>
</tr>
<tr>
<td>Action:</td>
<td>
<select name="action" value="0">
<option value="0">Choose a Action...</option>
<option value="create">Create</option>
<option value="read">Read</option>
<option value="update">Update</option>
<option value="delete">Delete</option>
<option value="fancy">Fancy Display</option>
<option value="pass">Pass to JSP File</option>
</select>
</td>
</tr>
<tr>
<td>Record ID:</td>
<td><input type="text" name="tid" size="3"/></td>
</tr>
<tr>
<td>
First Name:
</td>
<td>
<input type="text" name="firstname" size="30"/>
</td>
</tr>
<tr>
<td>
Last Name:
</td>
<td>
<input type="text" name="lastname" size="30"/>
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="hidden" name="name" value="<jsp:getProperty name="myBean" property="name" />" />
<input type="hidden" name="username" value="<jsp:getProperty name="myBean" property="username" />" />
<input type="hidden" name="screenName" value="<jsp:getProperty name="myBean" property="screenName" />" />
<input type="hidden" name="password" value="<jsp:getProperty name="myBean" property="password" />" />
</td>
</tr>
</tbody>
</table>
<input type="submit" value="Login" />
<input type="reset" name="clear" value="Clear" />
</form>
</body>
Related
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 :(
I have used Resource Bundle editor to create _hi(Hindi language) locale properties file in eclipse.
My problem is that if I run my webpage, it displays ???? instead of Hindi content.
I have provided code below. It works fine with french language
spring-servlet.xml
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="siteLang"></property>
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="WEB-INF/messages"></property>
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="ta"></property>
<property name="cookieName" value="BalsCookie"></property>
<property name="cookieMaxAge" value="300"></property>
</bean>
form.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!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" href="<spring:theme code='color'/>" type="text/css" />
</head>
<body>
Hindi
english
<center>
<h1>${heading}</h1>
</center>
<h1>head</h1>
<form action="/SpringMVCP4/submit" method="post">
<center>
<table>
<tr>
<td><spring:message code="label.name"></spring:message></td>
<td><input type="text" name="name"></input><span> <form:errors
path="student.name"></form:errors></span></td>
</tr>
<tr>
<td class="spring"><spring:message code="label.dob" /></td>
<td><input type="text" name="dob"></input></td>
</tr>
<tr>
<td><spring:message code="label.skillSet" /></td>
<td><select name="skillSet" multiple>
<option>java</option>
<option>php</option>
<option>.net</option>
</select></td>
</tr>
<tr>
<td><spring:message code="label.mobileNo" /></td>
<td><input type="text" name="mobileNo"></input></td>
</tr>
<tr>
<td><spring:message code="label.street" /></td>
<td><input type="text" name="address.street"></input></td>
</tr>
<tr>
<td><spring:message code="label.city" /></td>
<td><input type="text" name="address.city"></input></td>
</tr>
<tr>
<td><spring:message code="label.pincode" /></td>
<td><input type="text" name="address.pincode"></input><span
style="color: red"> <form:errors
path="student.address.pincode" /></span></td>
</tr>
<tr>
<td><spring:message code="label.submit" var="submit" /></td>
<td><input type="submit" value="${submit}"></input></td>
</tr>
</table>
</center>
</form>
</body>
</html>
Please let me know the error.
Am trying to link from a JSP to a servlet . On clicking the button with the name="conf" I need to redirect to a servlet "/Initial" . The problem is when I use type="button" nothing happens, while when I use type="submit" the page gets directed to servlet "/Initial" and does the action there. Am not able to identify the problem.
Here is my code:
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# page import="reg.serv.*"%>
<!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 method="post">
<center>
<table border="1" width="30%" cellpadding="3">
<thead>
<tr>
<th colspan="2">Register Here</th>
</tr>
</thead>
<tbody>
<tr>
<td>Username</td>
<td><input type="text" class="" id="username" name="username1" value="" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password1" id="password" value="" /></td>
</tr>
<tr>
<td>Confirm Password</td>
<td><input type="password" name="confirmpassword1" id="confirmpassword" value="" /></td>
</tr>
<tr>
<td>Mobile Number</td>
<td><input type="text" class="" id="mob" name="mob1" value="" /></td>
</tr>
<tr>
<td>Email ID</td>
<td><input type="text" class="" id="email" name="email1" value=" " /></td>
</tr>
<tr>
<td>Address</td>
<td><textarea id="address" name="address1"></textarea></td>
</tr>
<tr>
<td colspan="2">Already registered Login Here</td>
</tr>
</tbody>
<tr>
<td><input type="button" value="confirm" name="conf" /></td>
<td><input type="reset" value="Reset" /></td>
<td><input type="button" value="Cancel" name="Cr" onclick="openPage('Initial.jsp')" /></td>
</tr>
</table>
</form>
<script type="text/javascript">
function openPage(pageURL) {
window.location = pageURL;
}
</script>
<%
String x = request.getParameter("conf");
if (x != null && x.equals("confirm")) {
//response.sendRedirect("/Initial");
RequestDispatcher dispatcher = request.getRequestDispatcher("/Initial");
dispatcher.forward(request, response);
}
%>
</body>
</html>
Please help me . Any help would be greatly appreciated. Thanking You.
you have to write
<form action=/your_servlet_page_name>
And you have to use
<input type="submit" value="confirm" name="conf"/>
And also you have to map your servlet page into web.xml file like
<servlet-mapping>
<servlet-name>CheckLogin</servlet-name>
<url-pattern>/CheckLogin</url-pattern>
</servlet-mapping>
<form action = "servlet-name" method = "method in the servlet">
<input type ="submit" value = "val">
</form>
This is a simple way to do this. If you are using the latest jre i think 7 and up, you don't need to declare the servlet in your web.xml file. the #WebServlet("/servlet-url") will do the trick.
if you want to use type="button" instead of type="submit". you can use javascript function on the click on the button. Like
<script>
function doSubmit(){
var actionURL ="MENTION URL YOU WANT TO REDIRECT";
// perform your operations
myForm.submit(actionURL); OR
myForm.submit();
}
</script>
<form name="myForm">
<input type="button" name="conf" value="conf" obclick="doSubmit();">
</form>
hope it will help you.
try by changing the script only
<script type="text/javascript">
function openPage(pageURL)
{
window.location.href = pageURL;
}
</script>
function openPage(pageURL) {
window.location = pageURL;
}
In above code snippet, pageURL has to be an absolute URL which in case of dealing with servlets may vary.So instead of this below can be used
location.href = (location.href).substr(0, (location.href).lastIndexOf('xyz.jsp'))+"/abc";
Here 'abc' is the servlet to which we have to redirect the 'xyz.jsp' .This can even work if there are many buttons.The respective functions could be written to redirect to respective servlets.
Also it works in case of input type is "button" or "submit".
I dont get exactly what you are trying to do,but redirect is working with:
response.sendRedirect(request.getContextPath());
or
response.sendRedirect(String url);
I have a struts2 form in which I added a Datepicker as a field. But after adding the DatePicker, when I submit my form, the Tomcat error msg shows..
HTTP Status 404 - No result defined for action com.abc.RecBackupConfigurationAction and result input
If I remove datepicker from my form, then everything runs fine. I don't know where I'm doing wrong.
My JSP:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%# taglib prefix="sj" uri="/struts-jquery-tags"%>
<%#taglib prefix="s" uri="/struts-tags" %>
<sj:head />
<script>
function grabDispositions(campaign) {
$.post("getCampDispositionsRecBackupConf", {campname: campaign}, function(response) {
$('#disposition_td').html(response);
});
}
</script>
<h3>Recording Backup Configuration</h3>
<div id="backup_conf">
<s:form theme="simple" action="saveDetailsRecBackupConf" method="post" name="viewConfiguration">
<!--Rule Name : <input name="rulename" id="rulename">-->
<table border="1">
<tr>
<td>Rule Name : </td>
<td><input name="rulename" id="rulename"></td>
<td>Frequency : </td>
<td>
<select id="frequency" name="frequency">
<option value="15">15 minutes</option>
<option value="30">30 minutes</option>
<option value="60">60 minutes</option>
<option value="120">120 minutes</option>
<option value="240">240 minutes</option>
<option value="480">480 minutes</option>
</select>
</td>
</tr>
<tr>
<td>Backup Server: </td>
<td><input name="backupserver"></td>
<td>Backup Directory: </td>
<td><input name="backupdir"></td>
</tr>
<tr>
<td>
Select Campaign :
</td>
<td>
<s:select id="campaign" headerKey="-1" headerValue="Select Campaign" list="campaignList" name="campname" theme="simple" onchange="grabDispositions(this.value)"/>
</td>
<td>
Select Dispositions :
</td>
<td id='disposition_td'>
<s:select id="dispositions" list="dispositionList" name="dispositions" multiple="true" theme="simple"/>
</td>
</tr>
<tr>
<td>Status : </td>
<td>
<select name="status">
<option value="active">Active</option>
<option value="inactive">InActive</option>
</select>
</td>
<td>Select Date : </td>
<td>
<sj:datepicker size="14" theme="simple" id="addedon" name="addedon" label="Select a Date/Time" timepicker="true" timepickerFormat="HH:mm:ss" displayFormat="yy-mm-dd" readonly="true"></sj:datepicker>
</td>
</tr>
</table>
<input type="submit" value="SAVE" name="SUBMIT" id="save" />
</s:form>
</div>
My struts-file mapping:
<action name="*RecBackupConf" method="{1}" class="com.abc.RecBackupConfigurationAction">
<result name="addConfigurationDetails" type="tiles">recBackupConfiguration</result>
<result name="saveDetails" type="tiles">viewRecordingConfiguration</result>
<result name="getCampDispositions">contaque/recBackup/dispositions.jsp</result>
<result name="displayDetails" >contaque/recBackup/editConfiguration.jsp</result>
</action>
How should I add the Datepicker to my form???
Following is my index.jsp:
<%# taglib prefix="sj" uri="/struts-jquery-tags"%>
<%# taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<s:head/>
<sj:head/>
<script type="text/javascript" src="jquery.validate.js"></script>
<script type="text/javascript" src="validate.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="style.css" media="all" />
<title>My Air - Home</title>
</head>
<body>
<div id="MainPage">
<jsp:include page="header.jsp"></jsp:include>
<div id="mainContent">
<jsp:include page="menubar.jsp"></jsp:include>
<div id="mainContentTop">
<div id="ContentLeft">
<jsp:include page="search.jsp"></jsp:include>
</div>
<div id="contentRight">
<jsp:include page="topdeals.jsp"></jsp:include>
</div>
</div>
</div>
<jsp:include page="footer.jsp"></jsp:include>
</div>
</body>
</html>
Following is my search.jsp:
<%# taglib prefix="s" uri="/struts-tags"%>
<%# taglib prefix="sj" uri="/struts-jquery-tags"%>\
<s:form action="searchFlight" theme="css_xhtml" cssClass="form1small" name="searchFlightForm">
<div class="searchHeadersmall">Search Flight</div>
<div id="searchForm1">
<s:actionerror/>
<s:select list="#application.cityList" headerValue="--------Select--------" headerKey="select" name="searchFlightDetails.from" label="Leaving From"></s:select>
<s:select list="#application.destlist" headerValue="---------Select-------" headerKey="select" name="searchFlightDetails.to" label="Going To"></s:select>
<sj:datepicker id="date0" label="Date" name="searchFlightDetails.dateoftravel" readonly="true" minDate="0"/>
<s:select list="#{'1':'1','2':'2','3':'3','4':'4','5':'5'}" headerValue="" headerKey="" name="searchFlightDetails.noofpassengers" label="No of tickets" style="width:50%; float:left"></s:select>
<div class="inputsmall nobottombordersmall">
<s:radio label="Type" name="searchFlightDetails.nonstop"
list="#{'Y':'NonStop','N':'Normal'}"/>
</div>
<div id="submitdivid1">
<sj:submit value="Show Flights" targets="mainContent"/>
</div>
</div>
</s:form>
When form is submitted the following searchFlightSuccess.jsp is loaded in mainContent <div>:
<%# taglib prefix="s" uri="/struts-tags"%>
<%# taglib prefix="sj" uri="/struts-jquery-tags"%>
<s:if test="#session.username!=null">
<jsp:include page="menubaruser.jsp"></jsp:include>
</s:if>
<s:if test="#session.username==null">
<jsp:include page="menubar.jsp"></jsp:include>
</s:if>
<div id="searchHeaderHistory">Search Results</div>
<div class="CSSTableGenerator">
<table>
<colgroup>
<col span="1" style="width: 5%;">
<col span="1" style="width: 10%">
<col span="1" style="width: 15%">
<col span="1" style="width: 10%">
<col span="1" style="width: 15%">
<col span="1" style="width: 15%">
<col span="1" style="width: 15%">
<col span="1" style="width: 5%">
<col span="1" style="width: 10%">
</colgroup>
<thead>
<tr>
<td colspan="9">From:<s:property
value="searchFlightDetails.from" />
To:<s:property value="searchFlightDetails.to" /></td>
</tr>
</thead>
<tbody>
<tr>
<td>Flight ID</td>
<td>Provider</td>
<td>Departure Source</td>
<td>Via</td>
<td>Via Arrival</td>
<td>Via Departure</td>
<td>Destination Arrival</td>
<td>Fare</td>
<td></td>
</tr>
<s:iterator value="searchedFlightsList" var="flights">
<tr>
<td align="center"><s:property
value="#flights.flightDetails.fid" /></td>
<td align="center"><s:property
value="#flights.flightDetails.providerDetails.pname" /></td>
<td align="center"><s:property value="#flights.sourcedepdate" />,<s:property
value="#flights.sourcedeptime" /> hrs</td>
<td align="center"><s:property
value="#flights.flightDetails.routeDetails.via" /></td>
<td align="center"><s:property value="#flights.viaarrdate" />,<s:property
value="#flights.viaarrtime" />hrs</td>
<td align="center"><s:property value="#flights.viadepdate" />,<s:property
value="#flights.viadeptime" />hrs</td>
<td align="center"><s:property value="#flights.destarrdate" />,<s:property
value="#flights.destarrtime" />hrs</td>
<td width="5%" align="center"><s:property
value="#flights.flightDetails.fares2d" /></td>
<td width="5%" align="center">
<s:form
action="passengerDetailsLink" method="get" theme="simple"
id="passengerDetailsLink_%{#flights.sid}">
<s:hidden value="%{#flights.sid}" name="sid" />
<s:hidden value="%{#flights.flightDetails.fares2d}" name="fare" />
<s:hidden value="%{searchFlightDetails.from}" name="from" />
<s:hidden value="%{searchFlightDetails.to}" name="to" />
<s:hidden value="%{searchFlightDetails.noofpassengers}"
name="passengers" />
<s:hidden value="1" name="flag" />
<sj:submit value="Book" targets="mainContent"
cssClass="orangebuttonsmall" />
</s:form></td>
</tr>
</s:iterator>
</tbody>
</table>
</div>
The scenario is as follows:
I entered some values in search.jsp. I submitted the form, and accordingly some results got loaded into the mainContent <div>. Since form was submitted using Struts 2 jQuery plugin submit , the URL remains the same and hence browser back button doesn't work. If I want to go back, how can I do that.
I read about some hash technique but , I am not able to apply it to this.
In the header tag of Struts2 jQuery use ajaxhistory attribute. Setting this attribute to true enabled browser history for Ajax requests. For details see wiki of header tag.