将值从jsp中的for循环传递到servlet

时间:2018-10-13 10:11:30

标签: java jsp servlets

嘿。我的Lego House应用程序中有一个“员工”网站,员工可以在其中滚动浏览所有客户的所有订单。在左侧,他可以看到一些已发货的订单,在右侧,可以看到一些未发货的订单。通过单击订单下方的按钮,订单将更改为“已发货”,并且将在我的数据库中更新。

enter image description here

在我的jsp站点中,我有一个for循环,遍历该列表中的所有订单。该列表包含每个订单的orderId。该orderId是我想要传递给我的servlet的东西。将orderId进一步传递给我的DataMapper,然后将我的订单从“未发货”更改为“已发货”。 但是,如何将正确的orderId从jsp端传递给servlet? 那就是我走了多远。

来自employee.jsp

<div id="notShipped">
    <h2>Orders not yet shipped</h2>
    <%
        for (OrderClass list : allOrders) {
            if (list.isShipped() == false) {
                out.println(list.toString());
                session.setAttribute("id", list.getOrderId());
    %> 
    <form action="FrontController" method="POST">
        <input type='hidden' name='command' value='sendOrder'/>
        <button type='submit'> Send order </button>
        <br>
        <%}
            }
        %>
        </div>
        </html>enter code here

将订单servlet发送到我想捕获orderId的位置,并将其进一步发送到我的datamapper:

public class SendOrder extends Command {

@Override
String execute(HttpServletRequest request, HttpServletResponse response) throws LoginSampleException, CalculationException {
    int orderId = (int) request.getSession().getAttribute("id");
    AdminMapper am = null;
    am.sendOrder(orderId);
    return "employeepage";
}
}

命令类:

abstract class Command {

private static HashMap<String, Command> commands;

private static void initCommands() {
    commands = new HashMap<>();
    commands.put("login", new Login());
    commands.put("register", new Register());
    commands.put("backdoor", new Backdoor());
    commands.put("measurements", new Order());
    commands.put("showorders", new ShowOrders());
    commands.put("checkout", new CheckOut());
    commands.put("previousorder", new PreviousOrder());
    commands.put("sendOrder", new SendOrder());
}

static Command from(HttpServletRequest request) {
    String commandName = request.getParameter("command");
    if (commands == null) {
        initCommands();
    }
    return commands.getOrDefault(commandName, new UnknownCommand());
}

abstract String execute(HttpServletRequest request, HttpServletResponse response)
        throws LoginSampleException, CalculationException;

}

前端控制器:

protected void processRequest( HttpServletRequest request, HttpServletResponse response )
        throws ServletException, IOException, CalculationException {
    try {
        Command action = Command.from( request );
        String view = action.execute( request, response );
        request.getRequestDispatcher( "/WEB-INF/" + view + ".jsp" ).forward( request, response );
    } catch ( LoginSampleException | CalculationException ex ) {
        request.setAttribute( "error", ex.getMessage());
        String currentSite = (String) request.getParameter("currentSite");
        String notIndex = "/WEB-INF/";
        if(currentSite.equals("index")){
            notIndex = "";
        }
        request.getRequestDispatcher(notIndex + currentSite +".jsp" ).forward( request, response );
    }
}

在此先感谢大家的帮助!

0 个答案:

没有答案