IndexAction.java package com.gym.user.action; import java.io.IOException; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.gym.user.service.impl.MatchServiceImpl; public class IndexAction extends HttpServlet { /** * The doGet method of the servlet. <br> * * 访问根目录默认跳转到index.jsp * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { MatchServiceImpl matchServiceImpl = new MatchServiceImpl(); List list = (List) matchServiceImpl.queryMatch(); RequestDispatcher dispatcher = request .getRequestDispatcher("index.jsp"); request.setAttribute("matchList", list); dispatcher.forward(request, response); } } 控制器中的doGet一般只负责显示页面,也就是调用各个模块的jsp文件;doPost一般只负责数据的处理,以及跳转到成功或失败页面。上面代码中的matchServiceImpl.queryMatch();就是调用赛事服务组件中的查询赛事方法,把返回的list对象放到request对象中,供jsp模板遍历输出,遍历代码如下: <c:forEach items="${requestScope.matchList}" var="list"> <li> <span class="title"><a href="match/index.html?mid=${list.getmId() }">${list.getmName()} </a> </span> <span class="time">${list.getmDate()}</span> </li> </c:forEach> 以上的控制器只是负责显示主页,但其他模块的控制器需要执行不同的操作,比如用户控制器,需要执行显示用户信息页面,显示修改密码页面,修改密码,修改个人信息等动作,这就需要一个标记来判断需要执行哪一个动作了。用String action = request.getParameter("action");获取用户需要执行的动作,然后判断if(action.equals("xxx")) { },分别写对应执行的代码即可。由于java1.6以及之前的版本都不支持switch中使用String来匹配,所以用了比较笨的办法,一个个判断,例如用户控制器:
UserAction.java ...... 本文转自:广东海洋大学体育馆管理系统 开发日记(11)——控制器(Action) - 云代码空间http://yuncode.net/article/a_5108a2e7b17f252
![](http://yuncode.net/upload/p_w_picpath/201301/30/20130130123423_15900.jpg)