`
cakin24
  • 浏览: 1336148 次
  • 性别: 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
分享到:
评论

相关推荐

    达内java培训目录

    Struts2 Struts2核心控制流程、Ognl、Action、Interceptor、Result、FreeMarker、Struts2标记库、Struts2扩展、Struts2应用技巧(输入验证、消息国际化、文件上传和下载、防止重复提交等)。 熟练掌握Struts2核心...

    李兴华Java Web开发实战经典(高清版) Part2

    15.5、深入Struts应用 15.6、本章摘要 15.7、开发实战讲解(基于Oracle数据库) 第16章 Struts常用标签库 16.1、Struts标签库简介 16.2、Bean标签 16.2.1、标签 16.2.2、标签 16.2.3、资源访问标签 ...

    java web 视频、电子书、源码(李兴华老师出版)

    15.5、深入Struts应用 15.6、本章摘要 15.7、开发实战讲解(基于Oracle数据库) 第16章 Struts常用标签库 16.1、Struts标签库简介 16.2、Bean标签 16.2.1、标签 16.2.2、标签 16.2.3、资源访问标签 ...

    李兴华 Java Web 开发实战经典_带源码_高清pdf 带书签 上

    15.5、深入Struts应用 15.6、本章摘要 15.7、开发实战讲解(基于Oracle数据库) 第16章 Struts常用标签库 16.1、Struts标签库简介 16.2、Bean标签 16.2.1、标签 16.2.2、标签 16.2.3、资源访问标签 16.2.4...

    MLDN+李兴华+Java+Web开发实战经典.part3.rar )

    15.5、深入Struts应用 15.6、本章摘要 15.7、开发实战讲解(基于Oracle数据库) 第16章 Struts常用标签库 16.1、Struts标签库简介 16.2、Bean标签 16.2.1、标签 16.2.2、标签 16.2.3、资源访问标签 ...

    李兴华 java_web开发实战经典 源码 完整版收集共享

    15.5、深入Struts应用 15.6、本章摘要 15.7、开发实战讲解(基于Oracle数据库) 第16章 Struts常用标签库 16.1、Struts标签库简介 16.2、Bean标签 16.2.1、标签 16.2.2、标签 16.2.3、资源访问标签 16.2.4...

    李兴华 Java Web 开发实战经典_带源码_高清pdf 带书签 下

    15.5、深入Struts应用 15.6、本章摘要 15.7、开发实战讲解(基于Oracle数据库) 第16章 Struts常用标签库 16.1、Struts标签库简介 16.2、Bean标签 16.2.1、标签 16.2.2、标签 16.2.3、资源访问标签 16.2.4...

    李兴华Java Web开发实战经典.pdf (高清版) Part1

    15.5、深入Struts应用 15.6、本章摘要 15.7、开发实战讲解(基于Oracle数据库) 第16章 Struts常用标签库 16.1、Struts标签库简介 16.2、Bean标签 16.2.1、标签 16.2.2、标签 16.2.3、资源访问标签 ...

    李兴华 Java Web 开发实战经典 高清扫描版Part3

    15.5、深入Struts应用 15.6、本章摘要 15.7、开发实战讲解(基于Oracle数据库) 第16章 Struts常用标签库 16.1、Struts标签库简介 16.2、Bean标签 16.2.1、标签 16.2.2、标签 16.2.3、资源访问标签 16.2.4...

    经典JAVA.EE企业应用实战.基于WEBLOGIC_JBOSS的JSF_EJB3_JPA整合开发.pdf

     国内知名的高端IT技术作家,已出版《Spring 2.0宝典》、《基于J2EE的Ajax宝典》、《轻量级J2EE企业应用实战》、《Struts 2权威指南》、《Ruby On Rails敏捷开发最佳实践》等著作。 目录: 第0章 学习Java...

Global site tag (gtag.js) - Google Analytics