接收表单参数-深入Struts2

阅读更多
一 接收表单参数的三种方式

接收表单参数-深入Struts2_第1张图片
 
 
二  第一方式——使用Action属性接收参数
1、login.jsp
<%@ page language = "java" import = "java.util.*" pageEncoding = "utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
 
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html >
  < head >
    < base href = " <%= basePath %> " >
   
    < title > My JSP 'login.jsp' starting page title >
   
         < meta http-equiv = "pragma" content = "no-cache" >
         < meta http-equiv = "cache-control" content = "no-cache" >
         < meta http-equiv = "expires" content = "0" >    
         < meta http-equiv = "keywords" content = "keyword1,keyword2,keyword3" >
         < meta http-equiv = "description" content = "This is my page" >
        
 
  head >
 
  < body >
         < form action = "LoginAction.action" method = "post" >
                用户名: < input type = "text" name = "username" >
                密码: < input type = "password" name = "password" >
                 < input type = "submit" value = "提交" />
         form >
  body >
html >
2、struts.xml文件
xml version = "1.0" encoding = "UTF-8" ?>
DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" " http://struts.apache.org/dtds/struts-2.1.dtd" ; >
< struts >
         < package name = "default" namespace = "/" extends = "struts-default" >
                 < default-action-ref name = "index" > default-action-ref >
                 < action name = "index" >
                         < result > /error.jsp result >
                 action >
                
                 < action name = "*_*_*" method = "{2}" class = "com.cakin.{3}.{1}Action" >
                         < result > /result.jsp result >
                         < result name = "add" > /{2}. jsp result >
                         < result name = "update" > /{2}. jsp result >
                 action >
                
                 < action name = "LoginAction" method = "login" class = "com.cakin.action.LoginAction" >
                         < result > /success.jsp result >
                 action >
         package >
struts >    
3、LoginAction.java
package com.cakin.action;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class LoginAction 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;
        }
 
         public String login(){
                System. out .println( username );
                 return SUCCESS ;
        }
}
4、success.jsp
<%@ page language = "java" import = "java.util.*" pageEncoding = "ISO-8859-1" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
 
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html >
  < head >
    < base href = " <%= basePath %> " >
   
    < title > My JSP 'success.jsp' starting page title >
   
         < meta http-equiv = "pragma" content = "no-cache" >
         < meta http-equiv = "cache-control" content = "no-cache" >
         < meta http-equiv = "expires" content = "0" >    
         < meta http-equiv = "keywords" content = "keyword1,keyword2,keyword3" >
         < meta http-equiv = "description" content = "This is my page" >
        
 
  head >
 
  < body >
    This is success JSP page. < br >
  body >
html >
5、测试

接收表单参数-深入Struts2_第2张图片
 
 
三  第二方式——使用Domain Model接收参数
1、login.jsp
<%@ page language = "java" import = "java.util.*" pageEncoding = "utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
 
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html >
  < head >
    < base href = " <%= basePath %> " >
   
    < title > My JSP 'login.jsp' starting page title >
   
         < meta http-equiv = "pragma" content = "no-cache" >
         < meta http-equiv = "cache-control" content = "no-cache" >
         < meta http-equiv = "expires" content = "0" >    
         < meta http-equiv = "keywords" content = "keyword1,keyword2,keyword3" >
         < meta http-equiv = "description" content = "This is my page" >
        
 
  head >
 
  < body >
         < form action = "LoginAction.action" method = "post" >
                用户名: < input type = "text" name = "user.username" >
                密码: < input type = "password" name = "user.password" >
                 < input type = "submit" value = "提交" />
         form >
  body >
html >
2、struts.xml文件
xml version = "1.0" encoding = "UTF-8" ?>
DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" " http://struts.apache.org/dtds/struts-2.1.dtd" ; >
< struts >
         < package name = "default" namespace = "/" extends = "struts-default" >
                 < default-action-ref name = "index" > default-action-ref >
                 < action name = "index" >
                         < result > /error.jsp result >
                 action >
                
                 < action name = "*_*_*" method = "{2}" class = "com.cakin.{3}.{1}Action" >
                         < result > /result.jsp result >
                         < result name = "add" > /{2}. jsp result >
                         < result name = "update" > /{2}. jsp result >
                 action >
                
                 < action name = "LoginAction" method = "login" class = "com.cakin.action.LoginAction" >
                         < result > /success.jsp result >
                 action >
         package >
struts >    
3、LoginAction.java
package com.cakin.action;
 
import com.cakin.po.User;
import com.opensymphony.xwork2.ActionSupport;
 
public class LoginAction extends ActionSupport {
 
         private User user ;
 
         public User getUser() {
                 return user ;
        }
 
         public void setUser(User user) {
                 this . user = user;
        }
 
         public String login(){
                System. out .println( user .getUsername());
                 return SUCCESS ;
        }
}
4、User.java
package com.cakin.po;
 
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;
        }
}
5、测试

接收表单参数-深入Struts2_第3张图片
 
 
四  第三方式——使用ModelDriven接收参数
1、login.jsp
<%@ page language = "java" import = "java.util.*" pageEncoding = "utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
 
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html >
  < head >
    < base href = " <%= basePath %> " >
   
    < title > My JSP 'login.jsp' starting page title >
   
         < meta http-equiv = "pragma" content = "no-cache" >
         < meta http-equiv = "cache-control" content = "no-cache" >
         < meta http-equiv = "expires" content = "0" >    
         < meta http-equiv = "keywords" content = "keyword1,keyword2,keyword3" >
         < meta http-equiv = "description" content = "This is my page" >
        
 
  head >
 
  < body >
         < form action = "LoginAction.action" method = "post" >
                用户名: < input type = "text" name = "username" >
                密码: < input type = "password" name = "password" >
                 < input type = "submit" value = "提交" />
         form >
  body >
html >
2、LoginAction.java
package com.cakin.action;
 
import com.cakin.po.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
 
public class LoginAction extends ActionSupport implements ModelDriven{
 
         private User user = new User();
 
         public String login(){
                System. out .println( user .getUsername());
                 return SUCCESS ;
        }
 
         @Override
         public User getModel() {
                 // TODO Auto-generated method stub
                 return user ;
        }
}
3、测试

接收表单参数-深入Struts2_第4张图片
 
 
五  综合——使用ModelDriven接收列表参数
1、login.jsp
<%@ page language = "java" import = "java.util.*" pageEncoding = "utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
 
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html >
  < head >
    < base href = " <%= basePath %> " >
   
    < title > My JSP 'login.jsp' starting page title >
   
         < meta http-equiv = "pragma" content = "no-cache" >
         < meta http-equiv = "cache-control" content = "no-cache" >
         < meta http-equiv = "expires" content = "0" >    
         < meta http-equiv = "keywords" content = "keyword1,keyword2,keyword3" >
         < meta http-equiv = "description" content = "This is my page" >
        
 
  head >
 
  < body >
         < form action = "LoginAction.action" method = "post" >
                用户名: < input type = "text" name = "username" >
                密码: < input type = "password" name = "password" >
                书籍1: < input type = "text" name = "booklist[0]" >
                书籍2: < input type = "text" name = "booklist[1]" >
                 < input type = "submit" value = "提交" />
         form >
  body >
html >
2、User.java
package com.cakin.po;
 
import java.util.List;
 
public class User {
         private String username ;
        
         private String password ;
        
         private List booklist ;
        
         public List getBooklist() {
                 return booklist ;
        }
         public void setBooklist(List booklist) {
                 this . booklist = booklist;
        }
         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;
        }
}
3、LoginAction.java
package com.cakin.action;
import com.cakin.po.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class LoginAction extends ActionSupport implements ModelDriven{
    private User user =new User();
    public String login(){
        System.out.println(user.getUsername());
        System.out.println(user.getBooklist().get(0));
        System.out.println(user.getBooklist().get(1));
        return SUCCESS;
    }
    @Override
    public User getModel() {
        // TODO Auto-generated method stub
        return user;
    }
}
4、测试

接收表单参数-深入Struts2_第5张图片
 
 
六  综合——使用ModelDriven接收对象参数
1、login.jsp
<%@ page language = "java" import = "java.util.*" pageEncoding = "utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
 
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html >
  < head >
    < base href = " <%= basePath %> " >
   
    < title > My JSP 'login.jsp' starting page title >
   
         < meta http-equiv = "pragma" content = "no-cache" >
         < meta http-equiv = "cache-control" content = "no-cache" >
         < meta http-equiv = "expires" content = "0" >    
         < meta http-equiv = "keywords" content = "keyword1,keyword2,keyword3" >
         < meta http-equiv = "description" content = "This is my page" >
        
 
  head >
 
  < body >
         < form action = "LoginAction.action" method = "post" >
                用户名: < input type = "text" name = "username" >
                密码: < input type = "password" name = "password" >
                书籍1: < input type = "text" name = "booklist[0].username" >
                书籍2: < input type = "text" name = "booklist[1].username" >
                 < input type = "submit" value = "提交" />
         form >
  body >
html >
2、User.java
package com.cakin.po;
 
import java.util.List;
 
public class User {
         private String username ;
        
         private String password ;
        
         private List booklist ;
        
         public List getBooklist() {
                 return booklist ;
        }
         public void setBooklist(List booklist) {
                 this . booklist = booklist;
        }
         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;
        }
}
3、LoginAction.java
package com.cakin.action;
import com.cakin.po.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class LoginAction extends ActionSupport implements ModelDriven{
    private User user =new User();
    public String login(){
        System.out.println(user.getUsername());
        System.out.println(user.getBooklist().get(0).getUsername());
        System.out.println(user.getBooklist().get(1).getUsername());
        return SUCCESS;
    }
    @Override
    public User getModel() {
        // TODO Auto-generated method stub
        return user;
    }
}
 
4、测试

 


接收表单参数-深入Struts2_第6张图片
 
  • 接收表单参数-深入Struts2_第7张图片
  • 大小: 210 KB
  • 接收表单参数-深入Struts2_第8张图片
  • 大小: 20.8 KB
  • 接收表单参数-深入Struts2_第9张图片
  • 大小: 21.5 KB
  • 接收表单参数-深入Struts2_第10张图片
  • 大小: 20.6 KB
  • 接收表单参数-深入Struts2_第11张图片
  • 大小: 24.3 KB
  • 接收表单参数-深入Struts2_第12张图片
  • 大小: 24.3 KB
  • 查看图片附件

你可能感兴趣的:(表单,Struts2)