在struts2中,action在MVC中充当模型还是控制器角色

当然是控制器了,里面虽然有相应的数据,并且有get 和set 方法,不过这些参数都是控制器进行控制转发处理逻辑的时候需要的数据,比如之前你单写Servlet的时候,Request和Response里面总要读取和填充数据吧?现在全部交由Struts2的Action组件来做了,懂否?比如下面的例子import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import com.dao.AdminDao;import com.dao.StudentDao;import com.dao.TeacherDao;import com.opensymphony.xwork2.ActionSupport;public class GoLogin extends ActionSupport {\t//下面是Action内用于封装用户请求参数和响应参数的属性\tprivate String Type;\tprivate String Username;\tprivate String Password;\tprivate String Msg;\tpublic String getType() {\t\treturn Type;\t}\tpublic void setType(String type) {\t\tType = type;\t}\tpublic String getUsername() {\t\treturn Username;\t}\tpublic void setUsername(String username) {\t\tUsername = username;\t}\tpublic String getPassword() {\t\treturn Password;\t}\tpublic void setPassword(String password) {\t\tPassword = password;\t}\tpublic String getMsg() {\t\treturn Msg;\t}\tpublic void setMsg(String msg) {\t\tMsg = msg;\t}\t//处理用户请求的execute方法\tpublic String execute() throws Exception {\t\t\t\tSystem.out.println(Username + " " + Password);\t\t\t\tif(Type.equals("系统管理员"))\t\t{\t\t\tif (null == new AdminDao().CheckLogin(Username, Password)) {\t\t\t\tMsg = "用户名或者密码错误";\t\t\t\treturn INPUT;\t\t\t}\t\t\telse\t\t\t{\t\t\t\t//获取ID\t\t\t\tString Admin_ID=new AdminDao().CheckLogin(Username, Password);\t\t\t\t//创建session\t\t\t\tHttpSession session = ServletActionContext.getRequest().getSession();\t\t\t\tsession.setAttribute("id", Admin_ID);\t\t\t\tsession.setAttribute("type", "1");\t\t\t\treturn SUCCESS;\t\t\t}\t\t}\t\telse if(Type.equals("楼宇管理员"))\t\t{\t\t\tif (null == new TeacherDao().CheckLogin(Username, Password)) {\t\t\t\tMsg = "用户名或者密码错误";\t\t\t\treturn INPUT;\t\t\t}\t\t\telse\t\t\t{\t\t\t\t//获取ID\t\t\t\tString Teacher_ID=new TeacherDao().CheckLogin(Username, Password);\t\t\t\t//创建session\t\t\t\tHttpSession session = ServletActionContext.getRequest().getSession();\t\t\t\tsession.setAttribute("id", Teacher_ID);\t\t\t\tsession.setAttribute("type", "2");\t\t\t\t\t\t\t\treturn SUCCESS;\t\t\t}\t\t}\t\telse if(Type.equals("学生"))\t\t{\t\t\tif (null == new StudentDao().CheckLogin(Username, Password)) {\t\t\t\tMsg = "用户名或者密码错误";\t\t\t\treturn INPUT;\t\t\t}\t\t\telse\t\t\t{\t\t\t\t//获取ID\t\t\t\tString Student_ID=new StudentDao().CheckLogin(Username, Password);\t\t\t\t//创建session\t\t\t\tHttpSession session = ServletActionContext.getRequest().getSession();\t\t\t\tsession.setAttribute("id", Student_ID);\t\t\t\tsession.setAttribute("type", "3");\t\t\t\treturn SUCCESS;\t\t\t}\t\t}\t\telse\t\t{\t\t\tMsg = "身份类型错误";\t\t\treturn INPUT;\t\t}\t\t\t}}Type UserName PassWord 本来应该从Request对象的getParameter方法中获取的,不过现在全部封装隐藏了,返回值msg也封装了,这是response中body的数据,就这样。。。。。
■网友
当然是控制器啦。拿action当模型用,是m和c没分开。
■网友


推荐阅读