我试图使用MVC编写Web应用程序。我有bean,jsp和servlets。 我想要实现的目标:用户将能够在表格中创建(购物)列表。该表将有一个显示创建日期的thead。然后它将创建包含3列的行(1-id,2-name of product,3- a' buy' link)。当用户点击“已购买”时,链接,它将从3列中删除链接,并将产品的名称变为绿色。 表外的某个地方会有另一个链接,让用户创建一个新列表。如果存在现有列表,则表示尚未购买的产品'然而,会自动转到新列表。一旦用户点击"创建新列表"链接,它只显示新列表(如果有的话,还有未购买的产品)。
我目前遇到的问题是:只有点击购买的链接才会显示日期,但点击后不会将文字变为绿色或删除"已购买的"链接。
这是我的代码,任何帮助将不胜感激。谢谢。
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 );
}
}
答案 0 :(得分:0)
以下链接应该替换为在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 );
}