框架第二天

weixin_34054866發表於2016-11-24

Action中基本的資料對應方式

  • 模型驅動(FieldDriven)
public class UserAction extends ActionSupport implements ModelDriven<User> {
    private User user = new User();// 不需要有set get方法,但要有空引數的構造方法
    @Override
    public String execute() throws Exception {
        if (user.getUser().equals("qq") && user.getPwd().equals("123")) {
            return SUCCESS;
        } else {
            return ERROR;
        }
    }
    public User getModel() {
        return user;
    }

}

動態呼叫

<action name="login" class="controller.T2">
login!方法名.action


http://localhost:8080/day1/qq/login!find.action

萬用字元

<action name="login_*" class="controller.UserAction" method="{1}">
<result name="success">/success.jsp</result>
<result name="error">/fail.jsp</result>
<result name="list">/list.jsp</result>
<result name="tolist" type="redirect">/login_list</result>
</action>


<a href="<%=request.getContextPath() %>/login_del">刪除</a>
   <a href="<%=request.getContextPath() %>/login_update">修改</a>

struts2中的servlet物件

 //struts中用 servlet的物件,以下為request session response application out
     HttpServletRequest request = ServletActionContext.getRequest();
     HttpSession session = request.getSession();
     HttpServletResponse response = ServletActionContext.getResponse();
     ServletContext servletContext = ServletActionContext.getServletContext();
     PrintWriter out = response.getWriter();