lundi 16 février 2015

Trying to set the input text field in HTML to a default value using Java

I'm having trouble setting the input value of my input text field using Java. I know in HTML you can use the value=" " but I don't want to always have the value set. The way my Web App is set up is that if a user wants to edit a certain piece of data, then that data should show up in the input field. Otherwise, the input field will be blank so the user can add a new piece of data.


However, I'm having trouble trying to figure out how to get it read the data and set it to the value.


This is my main page where the user chooses the option to Add/Update a hotel or a delete one.



<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://ift.tt/1J4cr1i">
<%@ taglib uri="http://ift.tt/QfKAz6" prefix="c" %>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hotel</title>
</head>
<body>

<h1>Hotels Avaliable:</h1>

<form method="POST" action="hController?action=addUpdate">
<table class="table table-striped">
<tr>
<th></th>
<th>Hotel Name</th>
<th>Hotel Address</th>
<th>City</th>
<th>State</th>
<th>Postal Code</th>
<th>Notes About the Hotel</th>
</tr>
<c:forEach var="hotel" items="${hotels}">
<tr>
<td><input type="checkbox" name="id" value="${hotel.hotel_id}" /></td>
<td>${hotel.hotel_name}</td>
<td>${hotel.street_address}</td>
<td>${hotel.city}</td>
<td>${hotel.state}</td>
<td>${hotel.postal_code}</td>
<td>${hotel.note}</td>
</tr>
</c:forEach>
</table>
<table>
<tr>
<td>
<button class="btn btn-primary" type="submit" name="addUpdate">Add/Update a Hotel</button>&nbsp;
</td>
<td>
<button class="btn btn-primary" type="submit" name="delete">Delete a Hotel</button>&nbsp;
</td>
</tr>
</table>
</form>
</body>


This page is the form used to either add or update. This is where I'm having trouble figuring out how to get the value if they select one of the hotels using the checkbox for the ID.



<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://ift.tt/1J4cr1i">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Add a Hotel</title>
</head>
<body>
<h1>Add a Hotel</h1>
<p>Fill out the form below and then click "Add a Hotel" to save the information.</p>
<br>

<form method="POST" action="hController?action=save">

Hotel Name: <input type="text" name="hotelName">
<br>
Street Address: <input type="text" name="streetAddress">
<br>
City: <input type="text" name="city">
<br>
State <input type="text" name="state">
<br>
Postal Code: <input type="text" name="postalCode">
<br>
Notes about the Hotel: <input type="text" name="note">
<br>
<button class="btn btn-primary" type="submit" name="save">Add a hotel</button>

</form>
</body>


Below is the Servlet running the code.



public class HotelController extends HttpServlet {
private static final String FIND_ACTION_KEY = "findAll";
private static final String ADD_UPDATE_ACTION_KEY = "addUpdate";
private static final String SAVE_ACTION_KEY = "save";
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, ClassNotFoundException, SQLException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
HotelService hs = new HotelService();
String action = request.getParameter("action");
String destination = "";

if (FIND_ACTION_KEY.equals(action)) {
List hotels = hs.findAllHotels();
request.setAttribute("hotels", hotels);
destination = "/hotel.jsp";
} else if (ADD_UPDATE_ACTION_KEY.equals(action)) {
String addUpdate = request.getParameter("addUpdate");
if (addUpdate != null) {
int idNum = Integer.parseInt(request.getParameter("id"));
if (request.getParameter("id") == null) {
Hotel hotel = new Hotel();
request.setAttribute("hotel", hotel);
destination = "/addUpdateHotel.jsp";
} else {
Hotel hotel = hs.findHotel(idNum);

request.setAttribute("hotel", hotel);
destination = "/addUpdateHotel.jsp";
}
} else {
String id = request.getParameter("id");
hs.removeHotel(hs.findHotel(Integer.parseInt(id)));
List hotels = hs.findAllHotels();
request.setAttribute("hotels", hotels);
destination = "/hotel.jsp";
}
} else if (SAVE_ACTION_KEY.equals(action)) {


String hotelName = request.getParameter("hotelName");
String streetAddress = request.getParameter("streetAddress");
String city = request.getParameter("city");
String state = request.getParameter("state");
String postalCode = request.getParameter("postalCode");
String note = request.getParameter("note");

Hotel hotel = new Hotel(hotelName, streetAddress, city, state, postalCode, note);

hs.addHotel(hotel);

List hotels = hs.findAllHotels();

request.setAttribute("hotels", hotels);
destination = "/hotel.jsp";
}


RequestDispatcher view = request.getRequestDispatcher(destination);
view.forward(request, response);
}
}


Every time I try to get the value by setting value="${hotel.hotel_name}", I get a blank page and nothing returns back.





Aucun commentaire:

Enregistrer un commentaire