ITeye博客

接收表单参数-深入Struts2

cakin24 · 2017-10-22 08:44 · Struts · 编程语言 · Markdown

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


 

 

二  第一方式——使用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">

        <!--

        <link rel="stylesheet" type="text/css" href="styles.css">

        -->

 

  </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">

        <!--

        <link rel="stylesheet" type="text/css" href="styles.css">

        -->

 

  </head>

 

  <body>

    This is success JSP page. <br>

  </body>

</html>

5、测试


 

 

三  第二方式——使用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">

        <!--

        <link rel="stylesheet" type="text/css" href="styles.css">

        -->

 

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


 

 

四  第三方式——使用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">

        <!--

        <link rel="stylesheet" type="text/css" href="styles.css">

        -->

 

  </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<User>{

 

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


 

 

五  综合——使用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">

        <!--

        <link rel="stylesheet" type="text/css" href="styles.css">

        -->

 

  </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<String> booklist;

        

        public List<String> getBooklist() {

                return booklist;

        }

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

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


 

 

六  综合——使用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">

        <!--

        <link rel="stylesheet" type="text/css" href="styles.css">

        -->

 

  </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<User> booklist;

        

        public List<User> getBooklist() {

                return booklist;

        }

        public void setBooklist(List<User> 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<User>{

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

 

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。