Monday 15 February 2010

java - Changing view (from MVC) using jsps -



java - Changing view (from MVC) using jsps -

im attempting write web application using mvc. have bean, jsp, , servlets. want accomplish: user able create (shopping)list in table. table have thead shows day created. create rows 3 columns(1-id,2-name of product, 3- 'bought' link). when user clicks on 'bought' link, remove link 3 column , turn name of product green. there link somewhere outside table allow user create new list. if there existing list, products have not been 'bought' yet, automatically go new list. 1 time user clicks "create new list" link, display new list(with unpurchased products if any).

i having issues with:the date not display until bought link clicked when clicked not turn text greenish or remove "bought" link.

here code, help appreciated. give thanks you.

item.java

import java.text.format; import java.text.simpledateformat; import java.util.date; public class item { //property names string name; integer id; boolean available = true; format formatter = new simpledateformat("mm/dd/yyyy"); string today = formatter.format(new date()); public item(integer id, string name, boolean available){ this.id = id; this.name = name; this.available = available; } public string gettoday() { homecoming today; } public integer getid() { homecoming id; } public void setid(integer id) { this.id = id; } public string getname() { homecoming name; } public void setname(string name) { this.name = name; } public boolean isavailable() { homecoming available; } public void setavailable(boolean available) { this.available = available; } }

displaylist.jsp

<?xml version="1.0" encoding="iso-8859-1" ?> <%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>grocery list</title> </head> <body> <h1>grocery list</h1> <table border='1'> <tr><th colspan='3'>${entry.today}</th></tr> <c:foreach items="${entries}" var="entry"> <tr> <td>${entry.id}</td> <c:choose> <c:when test="${entry.available != false }"> <td>${entry.name}</td> <td><a href="bought?id=${entry.id}">bought</a></td> </c:when> <c:when test="${entry.available != true }"> <td style="color:green;" >${entry.name}</td> <td></td> </c:when> </c:choose> </tr> </c:foreach> <tr> <form action="groceries" method="post"> <td colspan='2'><input type="text" name="product" /></td> <td><input type="submit" name="add" value="add" /></td> <p><input type="button" name="new" value="new list" /></p> </form> </tr> </table> </body> </html>

groceries.java

import java.io.ioexception; import java.util.arraylist; import java.util.list; import javax.servlet.servletconfig; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; @webservlet("/groceries") public class groceries extends httpservlet { private static final long serialversionuid = 1l; int y = 3; public groceries() { super(); } public void init(servletconfig config) throws servletexception { super.init( config ); list<item> entries = new arraylist<item>(); entries.add( new item(1, "milk", true) ); entries.add( new item(2, "soda", true) ); getservletcontext().setattribute( "entries", entries ); } protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { request.getrequestdispatcher("/web-inf/displaylist.jsp").forward(request, response); } @suppresswarnings("unchecked") protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string name = request.getparameter("product"); item entry = new item(y, name, true); y++; list<item> entries = (list<item>) getservletcontext().getattribute("entries"); entries.add( entry ); response.sendredirect("groceries"); } }

bought.java

import java.io.ioexception; import java.util.list; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; @webservlet("/bought") public class bought extends httpservlet { private static final long serialversionuid = 1l; public bought() { super(); } /** * given id, retrieve list entry. */ @suppresswarnings("unchecked") private item getentry( integer id ) { list<item> entries = (list<item>) getservletcontext().getattribute("entries" ); for( item entry : entries ) if( entry.getid().equals( id ) ) homecoming entry; homecoming null; } protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // entry edited integer id = integer.valueof( request.getparameter( "id" ) ); item entry = getentry( id ); // pass entry jsp using request scope request.setattribute( "entry", entry ); request.getrequestdispatcher( "/web-inf/displaylist.jsp" ).forward(request, response ); } protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // entry edited integer id = integer.valueof( request.getparameter( "id" ) ); item entry = getentry( id ); // alter entry based on user input entry.setavailable(true); // send user invitee book page request.getrequestdispatcher( "/web-inf/displaylist.jsp" ).forward(request, response ); } }

the next link should replace map servlet in jsp.

<a href="<c:url value='/bought'><c:param name='id' value='${entry.id}'/></c:url>">bought</a>

in doget method need set availability condition

protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // entry edited integer id = integer.valueof( request.getparameter( "id" ) ); item entry = getentry( id ); entry.setavailable(false); // pass entry jsp using request scope request.setattribute( "entry", entry ); request.getrequestdispatcher( "/web-inf/displaylist.jsp" ).forward(request, response ); }

java jsp servlets model-view-controller

No comments:

Post a Comment