📄 adddvdaction.java
字号:
/* * AddDVDAction.java * * */package com.dvd.controller;import com.dvd.model.DVDItem;import com.dvd.model.DVDLibrary;import java.util.LinkedList;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionMapping;import org.apache.struts.action.ActionForward;/** * * @author * @version */public class AddDVDAction extends Action { /* forward name="success" path="" */ private final static String SUCCESS = "success"; /** * This is the action called from the Struts framework. * @param mapping The ActionMapping used to select this instance. * @param form The optional ActionForm bean for this request. * @param request The HTTP Request we are processing. * @param response The HTTP Response we are processing. * @throws java.lang.Exception * @return */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { List errorMsgs = new LinkedList(); try { // Retrieve form parameters String title = request.getParameter("title"); String year = request.getParameter("year"); // Did the user type in a genre? String genre = request.getParameter("newGenre"); // If not, use the drop-down list value if ( (genre == null) || (genre.trim().length() == 0) ) { genre = request.getParameter("genre").trim(); } // Form verification if ( title == null || title.trim().length() == 0 ) { errorMsgs.add("Please enter the DVD title."); } if ( year == null || year.trim().length() == 0 ) { errorMsgs.add("Please enter the year of release for the DVD."); } else if ( !year.trim().matches("\\d\\d\\d\\d")) { errorMsgs.add("Please enter a valid year."); } if ( ! errorMsgs.isEmpty() ) { request.setAttribute("errorMsgs", errorMsgs); return mapping.findForward("error"); } // retrieve the "library" attribute from the session-scope HttpSession session = request.getSession(); DVDLibrary model = (DVDLibrary)session.getAttribute("library"); DVDItem item = model.addDVD(title, year, genre); // Store the item on the request-scope request.setAttribute("dvdItem", item); return mapping.findForward("success"); // Handle any unexpected expections } catch (RuntimeException e) { errorMsgs.add("An unexpected error: " + e.getMessage()); request.setAttribute("errorMsgs", errorMsgs); return mapping.findForward("error"); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -