我正在尝试使用 MVC 编写一个 Web 应用程序。我有 bean、JSP 和 servlet。
我想要完成的任务:用户将能够在表格中创建(购物)列表。该表的头部将显示其创建日期。然后它将创建具有 3 列的行(1-
id
、2-name of product
、3-“购买”链接)。当用户点击“购买”链接时,它将从第 3 列中删除该链接,并将产品名称变为绿色。
表外的某处将有另一个链接,可让用户创建新列表。如果已有列表,则尚未“购买”的产品将自动转到新列表。一旦用户点击“创建新列表”链接,它只会显示新列表(如果有未购买的产品)。
我目前遇到的问题是:只有在单击购买的链接后才会显示日期,但单击后不会将文本变为绿色或删除“已购买”链接。
这是我的代码,任何帮助将不胜感激。谢谢你。
项目.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;
}
}
显示列表.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>
杂货.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");
}
}
买了.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 );
}
}
应替换以下链接以在 JSP 中映射 servlet。
<a href="<c:url value='/Bought'><c:param name='id' value='${entry.id}'/></c:url>">Bought</a>
在
doGet
方法中您需要设置可用性条件
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 );
}