一 获取参数
页面
<form action="reg1" method="post">
username:<input type="text" name="username"><br>
password:<input type="password" name="pwd"><br>
repertPassword:<input type="password" name="pwd2"><br>
email:<input type="text" name="email"><br>
hobby:<input type="checkbox" name="agree" value="true"><br>
<input type="submit" value="submit">
</form>
1.依靠名字相同
public class LoginAction extends ActionSupport {
private String username;
private String pwd;
private String pwd2;
private String email;
private Boolean agree;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getPwd2() {
return pwd2;
}
public void setPwd2(String pwd2) {
this.pwd2 = pwd2;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Boolean getAgree() {
return agree;
}
public void setAgree(Boolean agree) {
this.agree = agree;
}
@Override
public String execute() throws Exception {
System.out.println("email:" +email);
System.out.println("agree:" + agree);
return Action.SUCCESS;
}
}
第二种,写个vo类,加上无参构造方法
public class User {
private String username;
private String pwd;
private String pwd2;
private String email;
private Boolean agree;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getPwd2() {
return pwd2;
}
public void setPwd2(String pwd2) {
this.pwd2 = pwd2;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Boolean getAgree() {
return agree;
}
public void setAgree(Boolean agree) {
this.agree = agree;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
}
写action
public class LoginAction2 extends ActionSupport implements ModelDriven{
private User user = new User();
@Override
public String execute() throws Exception {
System.out.println("user.email:" + user.getEmail());
System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<");
System.out.println("user.agree:" + user.getAgree());
return Action.SUCCESS;
}
public Object getModel() {
// TODO Auto-generated method stub
return user;
}
}
注意这样user会被压入request域的值栈中
第三种
vo类同上
action
public class LoginAction3 extends ActionSupport{
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String execute() throws Exception {
System.out.println("user.email:" + user.getEmail());
System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<");
System.out.println("user.agree:" + user.getAgree());
return Action.SUCCESS;
}
}
这种方式user不会被压入值栈中
注意:值栈中的对象只能引用其值,无法调用 如:
<body>
username:${username }<br>
password:${pwd }<br>
repertPassword:${pwd2 }<br>
<s:debug></s:debug>
</body>
二 域与Servlet中的三大对象
域action
(硬编码)
public class LoginAction extends ActionSupport {
private Map requestScope;
private Map sessionScope;
private Map applicationScope;
public LoginAction() {
requestScope = (Map) ActionContext.getContext().get("request");
sessionScope = ActionContext.getContext().getSession();
applicationScope = ActionContext.getContext().getApplication();
}
@Override
public String execute() throws Exception {
requestScope.put("aa", "aa");
sessionScope.put("user", new User("zhang","123","123","aad",true));
applicationScope.put("now", new Date());
return Action.SUCCESS;
}
}
当然sessionScope也可以通过requestScope的方式获得
(依赖注入)
public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
private Map<String,Object> requestScope;
private Map<String,Object> sessionScope;
private Map<String,Object> applicationScope;
@Override
public String execute() throws Exception {
requestScope.put("aa", "aa");
sessionScope.put("user", new User("zhang","123","123","aad",true));
applicationScope.put("now", new Date());
return Action.SUCCESS;
}
public void setApplication(Map<String, Object> arg0) {
// TODO Auto-generated method stub
this.applicationScope=arg0;
}
public void setSession(Map<String, Object> arg0) {
// TODO Auto-generated method stub
this.sessionScope=arg0;
}
public void setRequest(Map<String, Object> arg0) {
this.requestScope=arg0;
}
}
success页面
<body>
str:<s:property value="#request.aa"/>
user:<s:property value="#session.user.username"/>
now:<s:property value="#application.now"/>
<s:debug></s:debug>
</body>
#表示引用值栈中的对象若是request中的对象,还可以直接用el来表示如:${aa} 最重要的是通过debug查看其中信息
对象action
(硬编码)
public class ServletAPI extends ActionSupport {
private HttpServletResponse response;
private HttpServletRequest request;
private ServletContext application;
public ServletAPI() {
this.application = ServletActionContext.getServletContext();
this.request = ServletActionContext.getRequest();
this.response = ServletActionContext.getResponse();
}
@Override
public String execute() throws Exception {
response.setContentType("text/html;charset=utf-8");
response.setHeader("refresh", "3,url='http://www.baidu.com'");
PrintWriter out = response.getWriter();
out.print("<script type='text/javascript'>");
out.print("alert('aa');");
out.print("</script>");
return null;
}
}
(注入)
public class ServletAPI2 extends ActionSupport implements ServletRequestAware,ServletResponseAware,ServletContextAware{
private HttpServletResponse response;
private HttpServletRequest request;
private ServletContext application;
@Override
public String execute() throws Exception {
response.setContentType("text/html;charset=utf-8");
response.setHeader("refresh", "3,url='http://www.baidu.com'");
PrintWriter out = response.getWriter();
out.print("<script type='text/javascript'>");
out.print("alert('aa');");
out.print("</script>");
return null;
}
public void setServletContext(ServletContext arg0) {
// TODO Auto-generated method stub
this.application = arg0;
}
public void setServletResponse(HttpServletResponse arg0) {
this.response = arg0;
}
public void setServletRequest(HttpServletRequest arg0) {
this.request = arg0;
}
}
三 返回值讲四种
1 action-->jsp dispatcher不说了
2 action-->action chain
3 action -->jsp redirect
4 action-->action redirection
呵呵 写上突然发现没啥好说的 就是现象 试一下就知道了 只说一点
action--》action result 中写action名 不加“/”
四 全局配置
也很好理解 那就是当每个action都需要配置相同的东西的时候需要
举个简单的例子
<global-results>
<result name="error">/error.jsp</result>
</global-results>