`
cakin24
  • 浏览: 1384330 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

接收表单参数-深入Struts2

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


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

 



 
  • 大小: 210 KB
  • 大小: 20.8 KB
  • 大小: 21.5 KB
  • 大小: 20.6 KB
  • 大小: 24.3 KB
  • 大小: 24.3 KB
2
0
分享到:
评论

相关推荐

    第2讲 --Struts2的类型转换

    本讲将深入探讨Struts2的类型转换机制及其在实际开发中的应用。 首先,我们理解一下为什么需要类型转换。在HTTP请求中,所有的数据都是以字符串形式传递的。例如,用户通过表单提交的数据,服务器接收到的都是字符...

    struts2 接收参数

    这篇博客文章可能深入探讨了Struts2如何在Action类中获取和管理这些参数。 首先,Struts2的核心是DispatcherServlet,它负责拦截所有的HTTP请求,并根据配置的拦截器栈来处理请求。在Struts2中,Action类是业务逻辑...

    java--struts讲义

    - **struts-config.xml**:该文件是 Struts 的核心配置文件,它定义了 Action 映射、ActionForm 类、异常处理机制等。 - **web.xml**:Web 应用程序的部署描述符,其中包含了对 ActionServlet 的配置信息,如初始化...

    Struts2接收参数

    通过研究这些文件,你可以更深入地理解Struts2参数处理的机制。 总的来说,Struts2通过Action类、OGNL表达式和一系列的配置及标签,为开发人员提供了一个高效、灵活的参数接收和处理框架。在实际开发中,掌握这些...

    精通java Web开发--基于Struts EJB.zip

    Struts的控制器组件是ActionServlet,它接收HTTP请求,解析请求参数,并调用相应的Action。 EJB(Enterprise JavaBeans)是Java EE平台的核心部分,提供了服务器端组件模型,用于构建可部署的、可管理的、可移植的...

    面试知识点总结--struts面试题大全.pdf

    - 控制器(Controller):ActionServlet作为控制器,负责处理HTTP请求,解析配置文件(struts-config.xml)并调用相应Action类。 - 模型(Model):主要由JavaBeans构成,存储数据和业务逻辑。ActionServlet通过...

    洁净消毒餐具配送系统(----用struts实现------)

    Struts的核心组件包括Action类、配置文件(struts-config.xml)、ActionForm表单对象和JSP视图等。 2. **MVC设计模式**:Model-View-Controller模式是软件工程中的经典设计模式,用于解耦应用程序的各个部分。在这个...

    java struts2接收参数的几种方法

    本文将深入探讨Struts2接收参数的几种主要方法,包括通过Action的属性、使用Domain Model(领域模型)以及采用DTO(数据传输对象)进行参数接收。 ### 一、使用Action的属性接收参数 #### 原理 在Struts2框架中,...

    struts2中action接收参数的方式

    本篇文章将深入探讨Struts2中Action接收参数的多种方式,以及相关源码解析。 首先,最常见的接收参数方式是通过方法签名直接接收。例如,如果在JSP页面上有这样一个表单: ```jsp 提交" /&gt; ``` 对应的Action...

    应用Struts2处理表单数据

    每个Action对应一个特定的用户操作,负责接收表单数据并进行处理。开发者需要创建一个继承自`org.apache.struts2.dispatcher.ng.ExecuteOutcome`或`com.opensymphony.xwork2.ActionSupport`的自定义Action类。 2. *...

    struts2-blank

    6. **ActionForm/POJOs**:在Struts2中,Action通常与简单的Java Bean或ActionForm对象交互,用来接收和封装HTTP请求中的参数。 7. **依赖注入**:Struts2支持依赖注入(DI),可以通过注解或XML配置将服务注入到...

    sunxin-Struts2试读

    Struts2提供了一系列的标签,使得在JSP页面中操作数据更加方便,比如`s:property`标签用于显示Action属性的值,`s:form`标签用于创建表单,以及`s:iterator`标签用于遍历集合。此外,这一章可能还会讨论Struts2与...

    struts2代码演示

    Struts2是一个基于MVC(Model-View...通过这个压缩包中的代码示例,你可以深入理解Struts2的使用方式,包括Action的编写、配置文件的设定、拦截器的应用以及结果的处理等方面,进一步掌握Java Web开发中的MVC架构实践。

    struts1.2 from表单提交包含list的对象

    在Struts1.2中,这个请求会被Struts的ActionServlet捕获,ActionServlet会根据配置的Struts配置文件(struts-config.xml)来决定调用哪个Action类来处理请求。 对于包含列表的表单,用户可能需要在页面上输入多条...

    Struts2 技术内幕——深入解析Struts2架构设计与实现原理

    ### Struts2技术内幕——深入解析Struts2架构设计与实现原理 #### 一、Struts2概述 Struts2是Struts框架的第二代版本,它是在Struts1的基础上进行了大量的改进和完善后诞生的。Struts2不仅继承了Struts1的核心思想...

    毕业设计-基于struts的文章系统测试通过

    2. Struts工作原理:当用户发送HTTP请求到服务器时,Struts框架会通过ActionServlet拦截请求,然后根据配置文件(struts-config.xml)来决定哪个Action类应该处理该请求。Action类执行后,可能会更新模型,然后返回...

    使用Struts2的JSON插件来实现JSON数据传递

    &lt;package name="default" namespace="/" extends="struts-default,json-default"&gt; &lt;!-- Your action configurations here --&gt; ``` 启用插件后,你可以定义一个Struts2的动作(Action),并声明返回类型为`json`。...

    web-struts

    ActionForm用于封装表单数据,而配置文件struts-config.xml则定义了请求与Action的映射关系。 **2. Hibernate ORM框架** Hibernate是一个流行的Java持久化框架,能够简化数据库操作,通过对象-关系映射(ORM)技术...

    struts1.x 和 struts2.x向Action里填充jsp参数原理

    本篇文章将深入探讨Struts1.x和Struts2.x在向Action中填充JSP参数的原理。 Struts1.x的工作原理: Struts1的核心是ActionServlet,它是一个实现了Servlet接口的控制器。当用户发起HTTP请求时,请求会被Dispatcher...

Global site tag (gtag.js) - Google Analytics