简体   繁体   中英

Changing view (from MVC) using jsps

Im attempting to write a web application using MVC. I have the bean, the jsp, and servlets. What I want to accomplish: the user will be able to create a (shopping)list in a table. The table will have a thead that shows the day it was created. Then it will create rows with 3 columns(1-id,2-name of product, 3- a 'bought' link). When the user clicks on the 'bought' link, it will remove the link from the 3 column and will turn the name of the product green. There will be another link somewhere outside the table that will let the user create a new list. If there is an existing list, then the products that have not been 'bought' yet, would automatically go to the new list. Once the user clicks the "create new list" link, it will only display the new list(with unpurchased products if any).

I am currently having issues with:the date will not display only until the bought link is clicked but when clicked it will not turn the text green or remove the "bought" link.

here is my code, any help would be greatly appreciated. Thank 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() {
    return today;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public boolean isAvailable() {
    return 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 an id, retrieve the 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 ) ) 
            return entry;

    return null;
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // get the entry to be edited
    Integer id = Integer.valueOf( request.getParameter( "id" ) );
    Item entry = getEntry( id );

    // pass the entry to 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 {
    // get the entry to be edited
    Integer id = Integer.valueOf( request.getParameter( "id" ) );
    Item entry = getEntry( id );

    // change the entry based on user input
    entry.setAvailable(true);

    // send the user back to the guest book page
    request.getRequestDispatcher( "/WEB-INF/DisplayList.jsp" ).forward(request, response );
}

}

The following link should replace to map a servlet in JSP.

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

In the doGet method you need to set availability condition

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // get the entry to be edited
    Integer id = Integer.valueOf( request.getParameter( "id" ) );
    Item entry = getEntry( id );
    entry.setAvailable(false);
    // pass the entry to jsp using request scope
    request.setAttribute( "entry", entry );
    request.getRequestDispatcher( "/WEB-INF/DisplayList.jsp" ).forward(request, response );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM