struts2 Action获取表单传值(属性,类))

长平狐 发布于 2013/01/06 11:33
阅读 1K+
收藏 0
1.通过属性驱动式   
JSP:   
    <form action="sys/login.action" method="post">   
        <input type="text" name="username">   
        <input type="submit" value="submit">   
    </form>   
       
Action:直接通过get、set方法获取。   
  1. public class sysAction extends ActionSupport{     
  2.     private String username;     
  3.     
  4.     public String login() throws Exception {     
  5.         System.out.println(username);     
  6.         return SUCCESS;     
  7.     }     
  8.     
  9.     public String getUsername() {     
  10.         return username;     
  11.     }     
  12.     public void setUsername(String username) {     
  13.         this.username= username;     
  14.     }     
  15. }   
2.第二种方式类获取

JSP:   
    <form action="sys/login.action" method="post">   
        <input type="text" name="user.username">   
        <input type="text" name="teacher.level">   
        <input type="submit" value="submit">   
    </form>   
       
Action: 必须提供set方法   

  1. public class sysAction extends ActionSupport{     
  2.     private User user;     
  3.     private Teacher teacher;     
  4.     
  5.     public String login() throws Exception {     
  6.         System.out.println(user.getUsername());     
  7.         System.out.println(teacher.getLevel());     
  8.         return SUCCESS;     
  9.     }     
  10.     
  11.     public void setUser(User user) {     
  12.         this.user = user;     
  13.     }     
  14.     public void setTeacher(Teacher teacher) {     
  15.         this.teacher = teacher;     
  16.     }     
  17. }   

原文链接:http://blog.csdn.net/sd0902/article/details/8393157
加载中
返回顶部
顶部