---
title: "接收表单参数-深入Struts2"
author: "cakin24"
date: "2017-10-22T08:44:48Z"
tags: []
categories: ["编程语言"]
slug: "struts2"
draft: false
lastmod: "2017-10-22T08:44:48Z"
id: 2397212
url: "https://cakin24.iteye.com/blog/2397212"
---

一 接收表单参数的三种方式

![](http://dl2.iteye.com/upload/attachment/0127/4776/62352354-5736-30df-bb3c-5986bdd33dd6.png)  
 

 

二  第一方式——使用Action属性接收参数

1、login.jsp

&lt;%@ page language="java" import="java.util.\*" pageEncoding="utf-8"%&gt;

&lt;%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%&gt;

 

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;

&lt;html&gt;

  &lt;head&gt;

    &lt;base href="&lt;%=basePath%&gt;"&gt;

   

    &lt;title&gt;My JSP 'login.jsp' starting page&lt;/title&gt;

   

        &lt;meta http-equiv="pragma" content="no-cache"&gt;

        &lt;meta http-equiv="cache-control" content="no-cache"&gt;

        &lt;meta http-equiv="expires" content="0"&gt;   

        &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;

        &lt;meta http-equiv="description" content="This is my page"&gt;

        &lt;!--

        &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;

        --&gt;

 

  &lt;/head&gt;

 

  &lt;body&gt;

        &lt;form action="LoginAction.action" method="post"&gt;

                用户名：&lt;input type="text" name="username"&gt;

                密码：&lt;input type="password" name="password"&gt;

                &lt;input type="submit" value="提交" /&gt;

        &lt;/form&gt;

  &lt;/body&gt;

&lt;/html&gt;

2、struts.xml文件

&lt;?xml version="1.0" encoding="UTF-8" ?&gt;

&lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "[http://struts.apache.org/dtds/struts-2.1.dtd"](http://struts.apache.org/dtds/struts-2.1.dtd%22);&gt;

&lt;struts&gt;

        &lt;package name="default" namespace="/" extends="struts-default"&gt;

                &lt;default-action-ref name="index"&gt;&lt;/default-action-ref&gt;

                &lt;action name="index"&gt;

                        &lt;result&gt;/error.jsp&lt;/result&gt;

                &lt;/action&gt;

                

                &lt;action name="\*\_\*\_\*" method="{2}" class="com.cakin.{3}.{1}Action"&gt;

                        &lt;result&gt;/result.jsp&lt;/result&gt;

                        &lt;result name="add"&gt;/{2}.jsp&lt;/result&gt;

                        &lt;result name="update"&gt;/{2}.jsp&lt;/result&gt;

                &lt;/action&gt;

                

                &lt;action name="LoginAction" method="login" class="com.cakin.action.LoginAction"&gt;

                        &lt;result&gt;/success.jsp&lt;/result&gt;

                &lt;/action&gt;

        &lt;/package&gt;

&lt;/struts&gt;   

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

&lt;%@ page language="java" import="java.util.\*" pageEncoding="ISO-8859-1"%&gt;

&lt;%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%&gt;

 

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;

&lt;html&gt;

  &lt;head&gt;

    &lt;base href="&lt;%=basePath%&gt;"&gt;

   

    &lt;title&gt;My JSP 'success.jsp' starting page&lt;/title&gt;

   

        &lt;meta http-equiv="pragma" content="no-cache"&gt;

        &lt;meta http-equiv="cache-control" content="no-cache"&gt;

        &lt;meta http-equiv="expires" content="0"&gt;   

        &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;

        &lt;meta http-equiv="description" content="This is my page"&gt;

        &lt;!--

        &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;

        --&gt;

 

  &lt;/head&gt;

 

  &lt;body&gt;

    This is success JSP page. &lt;br&gt;

  &lt;/body&gt;

&lt;/html&gt;

5、测试

![](http://dl2.iteye.com/upload/attachment/0127/4778/4f384b3c-b421-3b9c-8f41-2ba23a114bdc.png)  
 

 

三  第二方式——使用Domain Model接收参数

1、login.jsp

&lt;%@ page language="java" import="java.util.\*" pageEncoding="utf-8"%&gt;

&lt;%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%&gt;

 

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;

&lt;html&gt;

  &lt;head&gt;

    &lt;base href="&lt;%=basePath%&gt;"&gt;

   

    &lt;title&gt;My JSP 'login.jsp' starting page&lt;/title&gt;

   

        &lt;meta http-equiv="pragma" content="no-cache"&gt;

        &lt;meta http-equiv="cache-control" content="no-cache"&gt;

        &lt;meta http-equiv="expires" content="0"&gt;   

        &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;

        &lt;meta http-equiv="description" content="This is my page"&gt;

        &lt;!--

        &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;

        --&gt;

 

  &lt;/head&gt;

 

  &lt;body&gt;

        &lt;form action="LoginAction.action" method="post"&gt;

                用户名：&lt;input type="text" name="user.username"&gt;

                密码：&lt;input type="password" name="user.password"&gt;

                &lt;input type="submit" value="提交" /&gt;

        &lt;/form&gt;

  &lt;/body&gt;

&lt;/html&gt;

2、struts.xml文件

&lt;?xml version="1.0" encoding="UTF-8" ?&gt;

&lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "[http://struts.apache.org/dtds/struts-2.1.dtd"](http://struts.apache.org/dtds/struts-2.1.dtd%22);&gt;

&lt;struts&gt;

        &lt;package name="default" namespace="/" extends="struts-default"&gt;

                &lt;default-action-ref name="index"&gt;&lt;/default-action-ref&gt;

                &lt;action name="index"&gt;

                        &lt;result&gt;/error.jsp&lt;/result&gt;

                &lt;/action&gt;

                

                &lt;action name="\*\_\*\_\*" method="{2}" class="com.cakin.{3}.{1}Action"&gt;

                        &lt;result&gt;/result.jsp&lt;/result&gt;

                        &lt;result name="add"&gt;/{2}.jsp&lt;/result&gt;

                        &lt;result name="update"&gt;/{2}.jsp&lt;/result&gt;

                &lt;/action&gt;

                

                &lt;action name="LoginAction" method="login" class="com.cakin.action.LoginAction"&gt;

                        &lt;result&gt;/success.jsp&lt;/result&gt;

                &lt;/action&gt;

        &lt;/package&gt;

&lt;/struts&gt;   

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、测试

![](http://dl2.iteye.com/upload/attachment/0127/4780/ca92c2b6-fa2d-3591-ba8e-d9cc905ed930.png)  
 

 

四  第三方式——使用ModelDriven接收参数

1、login.jsp

&lt;%@ page language="java" import="java.util.\*" pageEncoding="utf-8"%&gt;

&lt;%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%&gt;

 

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;

&lt;html&gt;

  &lt;head&gt;

    &lt;base href="&lt;%=basePath%&gt;"&gt;

   

    &lt;title&gt;My JSP 'login.jsp' starting page&lt;/title&gt;

   

        &lt;meta http-equiv="pragma" content="no-cache"&gt;

        &lt;meta http-equiv="cache-control" content="no-cache"&gt;

        &lt;meta http-equiv="expires" content="0"&gt;   

        &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;

        &lt;meta http-equiv="description" content="This is my page"&gt;

        &lt;!--

        &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;

        --&gt;

 

  &lt;/head&gt;

 

  &lt;body&gt;

        &lt;form action="LoginAction.action" method="post"&gt;

                用户名：&lt;input type="text" name="username"&gt;

                密码：&lt;input type="password" name="password"&gt;

                &lt;input type="submit" value="提交" /&gt;

        &lt;/form&gt;

  &lt;/body&gt;

&lt;/html&gt;

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&lt;User&gt;{

 

        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、测试

![](http://dl2.iteye.com/upload/attachment/0127/4782/9579176c-af22-3d4d-a706-5851a8c1111c.png)  
 

 

五  综合——使用ModelDriven接收列表参数

1、login.jsp

&lt;%@ page language="java" import="java.util.\*" pageEncoding="utf-8"%&gt;

&lt;%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%&gt;

 

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;

&lt;html&gt;

  &lt;head&gt;

    &lt;base href="&lt;%=basePath%&gt;"&gt;

   

    &lt;title&gt;My JSP 'login.jsp' starting page&lt;/title&gt;

   

        &lt;meta http-equiv="pragma" content="no-cache"&gt;

        &lt;meta http-equiv="cache-control" content="no-cache"&gt;

        &lt;meta http-equiv="expires" content="0"&gt;   

        &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;

        &lt;meta http-equiv="description" content="This is my page"&gt;

        &lt;!--

        &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;

        --&gt;

 

  &lt;/head&gt;

 

  &lt;body&gt;

        &lt;form action="LoginAction.action" method="post"&gt;

                用户名：&lt;input type="text" name="username"&gt;

                密码：&lt;input type="password" name="password"&gt;

                书籍1：&lt;input type="text" name="booklist\[0]"&gt;

                书籍2：&lt;input type="text" name="booklist\[1]"&gt;

                &lt;input type="submit" value="提交" /&gt;

        &lt;/form&gt;

  &lt;/body&gt;

&lt;/html&gt;

2、User.java

package com.cakin.po;

 

import java.util.List;

 

public class User {

        private String username;

        

        private String password;

        

        private List&lt;String&gt; booklist;

        

        public List&lt;String&gt; getBooklist() {

                return booklist;

        }

        public void setBooklist(List&lt;String&gt; 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&lt;User&gt;{

    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、测试

![](http://dl2.iteye.com/upload/attachment/0127/4784/306697b3-ef09-30f9-a2f4-24696d79e289.png)  
 

 

六  综合——使用ModelDriven接收对象参数

1、login.jsp

&lt;%@ page language="java" import="java.util.\*" pageEncoding="utf-8"%&gt;

&lt;%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%&gt;

 

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;

&lt;html&gt;

  &lt;head&gt;

    &lt;base href="&lt;%=basePath%&gt;"&gt;

   

    &lt;title&gt;My JSP 'login.jsp' starting page&lt;/title&gt;

   

        &lt;meta http-equiv="pragma" content="no-cache"&gt;

        &lt;meta http-equiv="cache-control" content="no-cache"&gt;

        &lt;meta http-equiv="expires" content="0"&gt;   

        &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;

        &lt;meta http-equiv="description" content="This is my page"&gt;

        &lt;!--

        &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;

        --&gt;

 

  &lt;/head&gt;

 

  &lt;body&gt;

        &lt;form action="LoginAction.action" method="post"&gt;

                用户名：&lt;input type="text" name="username"&gt;

                密码：&lt;input type="password" name="password"&gt;

                书籍1：&lt;input type="text" name="booklist\[0].username"&gt;

                书籍2：&lt;input type="text" name="booklist\[1].username"&gt;

                &lt;input type="submit" value="提交" /&gt;

        &lt;/form&gt;

  &lt;/body&gt;

&lt;/html&gt;

2、User.java

package com.cakin.po;

 

import java.util.List;

 

public class User {

        private String username;

        

        private String password;

        

        private List&lt;User&gt; booklist;

        

        public List&lt;User&gt; getBooklist() {

                return booklist;

        }

        public void setBooklist(List&lt;User&gt; 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&lt;User&gt;{

    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、测试

 

![](http://dl2.iteye.com/upload/attachment/0127/4786/785d32b4-7cc0-39e1-aac4-6818c5cf38bf.png)