标签:index tle else tac throws package net public xtend
1.属性驱动? a.直接在 action 类中提供与请求参数匹配属性,提供 get/set 方法? b.在 action 类中创始一个 javaBean,对其提供 get/set ,在请求时页面上要进行修改,? 例如 user.username user.password ,要使用 ognl 表达式? 以上两种方式的优缺点:? 第一种比较简单,在实际操作我们需要将 action 的属性在赋值给模型(javaBean)去操作? 第二种:不需要在直接将值给 javaBean过程,因为直接将数据封装到了 javaBean? 中。它要 求在页面上必须使用 ognl 表达式,就存在页面不通用问题。2.模型驱动? 步骤:? 1.让 Action 类要实现一个指定接口 ModelDriven? 2.实例化模型对象(就是要 new 出来 javaBean)? 3.重写 getModel 方法将实例化的模型返回
public class Login3Action extends ActionSupport implements ModelDriven<User> {
private User user = new User();
public User getModel() {
return user;
}
}
优点:解决了属性驱动存在的问题
缺点:一次只能封装一个model对象.
以下为三个案例的struts.xml文件的内容:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> ? <struts> <!-- 开启开发模式 --> <constant name="struts.devMode" value="true" /> <package name="p1" extends="struts-default"> <!-- 配置一个动作 --> <action name="sayHello" class="com.itheima.action.HelloAction" method="sayHello"> <!-- 配置结果视图 --> <result name="success">/success.jsp</result> </action> </package> ? <package name="default" namespace="/" extends="struts-default"> <action name="login" class="cn.itcast.action.LoginAction"> <result name="failer">/login.jsp</result> <result type="redirect">/success.jsp</result> </action> ? <action name="login1" class="cn.itcast.action.Login1Action"> <result name="failer">/login1.jsp</result> <result type="redirect">/success.jsp</result> </action> ? <action name="login2" class="cn.itcast.action.Login2Action"> <result name="failer">/login2.jsp</result> <result type="redirect">/success.jsp</result> </action> <action name="login3" class="cn.itcast.action.Login3Action"> <result name="failer">/login3.jsp</result> <result type="redirect">/success.jsp</result> </action> </package> </struts>
方式一案例:
Login1Action.Java文件:
package cn.itcast.action; ? import javax.servlet.http.HttpServletRequest; ? import org.apache.struts2.ServletActionContext; ? import com.opensymphony.xwork2.ActionSupport; ? //获取请求参数 属性驱动 第一种,直接将action做为model public class Login1Action extends ActionSupport { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } ? @Override public String execute() throws Exception { HttpServletRequest request = ServletActionContext.getRequest(); // 2.判断用户名与密码是否正确 if ("tom".equals(username) && "123".equals(password)) { request.getSession().setAttribute("username", username); return SUCCESS; } else { request.setAttribute("login.message", "用户名或密码错误"); return "failer"; } } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP ‘index.jsp‘ starting page</title> </head> <body> ${requestScope["login.message"] }<br> <form action="${pageContext.request.contextPath}/login1" method="post"> username:<input type="text" name="username"><br> password:<input type="password" name="password"><br> <input type="submit" value="登录"> </form> </body> </html>
方式二案例:
Login2Action.java文件
package cn.itcast.action; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import cn.itcast.domain.User; import com.opensymphony.xwork2.ActionSupport; //获取请求参数 属性驱动 第二种,直接在action声明一个model public class Login2Action extends ActionSupport { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public String execute() throws Exception { HttpServletRequest request = ServletActionContext.getRequest(); // 2.判断用户名与密码是否正确 if ("tom".equals(user.getUsername()) && "123".equals(user.getPassword())) { request.getSession().setAttribute("username", user.getUsername()); return SUCCESS; } else { request.setAttribute("login.message", "用户名或密码错误"); return "failer"; } } }
User.java文件:
package cn.itcast.domain; public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [username=" + username + ", password=" + password + "]"; } }
login2.jsp文件:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP ‘index.jsp‘ starting page</title> </head> <body> ${requestScope["login.message"] }<br> <form action="${pageContext.request.contextPath}/login2" method="post"> username:<input type="text" name="user.username"><br> password:<input type="password" name="user.password"><br> <input type="submit" value="登录"> </form> </body> </html>
方式三案例: Login3Action.java文件:
package cn.itcast.action; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import cn.itcast.domain.User; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; //获取请求参数 模型驱动 public class Login3Action extends ActionSupport implements ModelDriven<User> { private User user = new User(); public User getModel() { return user; } @Override public String execute() throws Exception { HttpServletRequest request = ServletActionContext.getRequest(); // 2.判断用户名与密码是否正确 if ("tom".equals(user.getUsername()) && "123".equals(user.getPassword())) { request.getSession().setAttribute("username", user.getUsername()); return SUCCESS; } else { request.setAttribute("login.message", "用户名或密码错误"); return "failer"; } } }
login3.jsp文件:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP ‘index.jsp‘ starting page</title> </head> <body> ${requestScope["login.message"] }<br> <form action="${pageContext.request.contextPath}/login3" method="post"> username:<input type="text" name="username"><br> password:<input type="password" name="password"><br> <input type="submit" value="登录"> </form> </body> </html>
标签:index tle else tac throws package net public xtend
原文地址:http://www.cnblogs.com/webmark2016/p/7123539.html