标签:type action odi void password tor utf-8 pack post
长话短说,直接进入主题.
1.属性驱动
package com.tbamaw.web.action; import com.opensymphony.xwork2.ActionSupport; public class User2Action extends ActionSupport{ private static final long serialVersionUID = 7248487833146321912L; private String username; //属性名称必须和jsp里面的input中的name值相同 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; } public String login() { System.out.println(username+"==="+password); return NONE; } }
<form method="post" action="${pageContext.request.contextPath }/user_login.action" > <table> <tr> <td>用户名:</td> <td><input type="text" name="username"></td> </tr> <tr> <td>密码</td> <td><input type="password" name="password"></td> </tr> <tr> <td colspan="2"><input type="submit" value="提交"></td> </tr> </table> </form>
2.模型驱动
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7 <title>登录</title>
8 </head>
9 <body>
10 <form method="post" action="${pageContext.request.contextPath }/user_login.action" >
11 <table>
12 <tr>
13 <td>用户名:</td>
14 <td><input type="text" name="username"></td>
15 </tr>
16 <tr>
17 <td>密码</td>
18 <td><input type="password" name="password"></td>
19 </tr>
20 <tr>
21 <td colspan="2"><input type="submit" value="提交"></td>
22 </tr>
23 </table>
24 </form>
25 </body>
26 </html>
1 package com.tbamaw.web.action;
2
3 import com.opensymphony.xwork2.ActionSupport;
4 import com.opensymphony.xwork2.ModelDriven;
5 import com.tbamaw.domain.User;
6 /**
7 * 模型驱动方式
8 * @author Administrator
9 */
10 public class UserAction extends ActionSupport implements ModelDriven<User>{
11
12 private static final long serialVersionUID = -5185250590002027078L;
13 private User user = new User(); //必须new对象
14 public User getModel() {
15 return user;
16 }
17 public String login() {
18 System.out.println(user.getUsername()+"--"+user.getPassword());
19 return NONE;
20 }
21 }
实体类
1 public class User { 2 private int uid; 3 private String username; 4 private String password; 5 public int getUid() { 6 return uid; 7 } 8 public void setUid(int uid) { 9 this.uid = uid; 10 } 11 public String getUsername() { 12 return username; 13 } 14 public void setUsername(String username) { 15 this.username = username; 16 } 17 public String getPassword() { 18 return password; 19 } 20 public void setPassword(String password) { 21 this.password = password; 22 } 23 }
3.表达式封装
public class User3Action extends ActionSupport{ private static final long serialVersionUID = -2278224171908902139L; private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String login() { System.out.println(user.getUsername()+"=="+user.getPassword()); return NONE; } }
<form method="post" action="${pageContext.request.contextPath }/user_login.action" > <table> <tr> <td>用户名:</td> <td><input type="text" name="user.username"></td> </tr> <tr> <td>密码</td> <td><input type="password" name="user.password"></td> </tr> <tr> <td colspan="2"><input type="submit" value="提交"></td> </tr> </table> </form>
标签:type action odi void password tor utf-8 pack post
原文地址:http://www.cnblogs.com/bamaw/p/7236144.html