package cn.bdqn.bean;

/**
*
*用户的实体类
*/
public class User { private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(String name, String password) {
super();
this.name = name;
this.password = password;
}
public User() {
super();
} }

User实体类

一:前台传递单个属性

01.创建登录界面

<%@ 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 'index.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="user/login" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="登录"/></td>
</tr>
</table>
</form>
</body>
</html>

02.在struts.xml中配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts> <package name="default" namespace="/user" extends="struts-default">
<!--用户登录的action -->
<action name="login" class="cn.bdqn.action.LoginAction">
<!--如果返回值是success,则可以省略name属性 -->
<result name="success">/loginSuccess.jsp</result>
</action>
</package>
</struts>

03.创建对应的Action

package cn.bdqn.action;

import cn.bdqn.bean.User;

import com.opensymphony.xwork2.ActionSupport;
/**
*
* 用户登录的action
*/
public class LoginAction extends ActionSupport { /**
* 01.定义和表单中name属性值一致的成员变量,并且具有get和set
*/
private String name;
private String password; @Override
public String execute() throws Exception {
System.out.println("用户名:"+name);
System.out.println("密码:"+password);
return "success"; //"success" SUCCESS super.execute()
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }

04.创建登录成功 页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <%@ taglib prefix="s" uri="/struts-tags" %>
<!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>
=============== 通过el表达式获取后台的数据 默认的作用是 request===============<br/>
${requestScope.name} <br/>
${password}<br/> =============== 通过struts2的标签获取===============<br/>
<s:property value="name"/><br/>
<s:property value="password"/><br/> <s:debug/> </body>
</html>

二:前台传递对象

修改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 'index.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="user/login" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="user.name"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="user.password"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="登录"/></td>
</tr>
</table>
</form>
</body>
</html>

02.struts.xml文件中的代码不需要改变

03.修改LoginAction中的代码

package cn.bdqn.action;

import cn.bdqn.bean.User;

import com.opensymphony.xwork2.ActionSupport;
/**
*
* 用户登录的action
*/
public class LoginAction extends ActionSupport { /**
* 02.直接定义前台表单中的对象!并且具有get和set! 这是我们常使用的方式
*/
private User user; @Override
public String execute() throws Exception {
System.out.println("用户名:"+user.getName());
System.out.println("密码:"+user.getPassword());
return "success"; //"success" SUCCESS super.execute()
} public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} }

04.修改loginSuccess.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <%@ taglib prefix="s" uri="/struts-tags" %>
<!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>
=============== 通过el表达式获取后台的数据 默认的作用是 request===============<br/>
${requestScope.user.name} <br/>
${user.password}<br/> =============== 通过struts2的标签获取===============<br/>
<s:property value="user.name"/><br/>
<s:property value="user.password"/><br/> <s:debug/> </body>
</html>

三:实现ModelDriven接口

其他代码和单个属性传递 一致!

只需要更改LoginAction中的代码即可!

package cn.bdqn.action;

import cn.bdqn.bean.User;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
* 用户登录的action
*/
public class LoginAction extends ActionSupport implements ModelDriven { /**
* 03.实现ModelDriven 前台的name属性值 必须要和user对象中的属性名一致!不需要user.
*/
private User user=new User(); @Override
public String execute() throws Exception {
System.out.println("用户名:"+user.getName());
System.out.println("密码:"+user.getPassword());
return "success"; //"success" SUCCESS super.execute()
} @Override
public Object getModel() {
return user;
} }

Struts.xml文件中一些常量的设置以及验证默认转发

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- struts2的默认扩展名
struts.action.extension=action,,
01.以action结尾 02.什么都不写
<constant name="struts.action.extension" value="do,t10"/> ### - struts.i18n.reload = true
### - struts.configuration.xml.reload = true
struts.devMode = false //手动重启 01.开发模式: 修改代码多 struts.devMode =true 自动重启
02.生产模式: 项目发布 struts.devMode = false 手动重启
-->
<constant name="struts.devMode" value="true"/><!-- 设置开发模式 -->
<package name="default" namespace="/user" extends="struts-default">
<!--用户登录的action -->
<action name="login" class="cn.bdqn.action.LoginAction">
<!--如果返回值是success,则可以省略name属性
type:的默认值 是转发!
怎么验证转发?
看浏览器中的url是不是action的属性值!是!就是转发!
如果是 loginSuccess.jsp说明是重定向!
type="redirect":重定向!
-->
<result name="success">/loginSuccess.jsp</result>
</action>
</package>
</struts>

Struts03---参数传递的更多相关文章

  1. js学习之函数的参数传递

    我们都知道在 ECMAScript 中,数据类型分为原始类型(又称值类型/基本类型)和引用类型(又称对象类型):这里我将按照这两种类型分别对函数进行传参,看一下到底发生了什么. 参数的理解 首先,我们 ...

  2. kettle中含有参数传递的定时任务

    (1)新建一个作业(新建->作业),并在控制面板右键: (2)设置一个命令参数: (3)把作业的参数传递给转换: (4)在转换中右键设置转换属性: (5)接收作业中设置的传递参数: (6)参数的 ...

  3. Java基础知识笔记(七:接口、变量作用域和参数传递)

    一.接口 Java语言不允许一个子类拥有多个直接父类,即任何子类只能有一个直接父类.但允许一个类实现多个接口,即在定义类的接口名称列表中可以包含1个或多个接口名称,从而实现多重继承的特性.接口的定义格 ...

  4. shell 脚本之获取命令输出字符串以及函数参数传递

    在ubuntu 14.04之后,所有的U盘挂载也分用户之分,最近很多操作也和U盘有关,所以就研究了一上午shell脚本函数以及字符串操作的方法. 字符串操作: 获取他的命令输出比较简单,打个简单的比方 ...

  5. 【GoLang】golang 闭包 closure 参数传递的蹊跷!

    结论: 闭包函数可以直接引用外层代码定义的变量, 但是,注意,闭包函数里面引用的是变量的地址, 当goroutine被调度时,改地址的值才会被传递给goroutine 函数. 介绍 go的闭包是一个很 ...

  6. JQuery Mobile 页面参数传递

    在单页模版中使用基于HTTP的方式通过POST和GET请求传递参数,而在多页模版中不需要与服务器进行通信,通常在多页模版中有以下三种方法来实现页面间的参数传递. 1.GET方式:在前一个页面生成参数并 ...

  7. Java 中的值传递和参数传递

    Java中没有指针,所以也没有引用传递了,仅仅有值传递不过可以通过对象的方式来实现引用传递 类似java没有多继承 但可以用多次implements 接口实现多继承的功能 值传递:方法调用时,实际参数 ...

  8. 【Python学习】函数参数传递方法四种(位置,关键字,默认值,包裹位置,包裹关键字传递)

    1. 位置传递: #--coding:utf-8-- def send(name,address): return 'package is sent to %s, located in %s' %(n ...

  9. JAVA反射参数传递

    引用:http://fish2700.blog.163.com/blog/static/130713192009103035723281/ 使用Method反射调用函数时,我们通常会遇到以下几种情况: ...

  10. [蟒蛇菜谱]Python函数参数传递最佳实践

    将函数作为参数传递,同时将该函数需要的参数一起传递.可参考threading.Timer的处理方式: class threading.Timer(interval, function, args=[] ...

随机推荐

  1. String StringBuffer StringBuilder 老生常谈

    1.String 与 StringBuffer . StringBuilder的区别 String 字符串常量 而 (StringBuffer 和 StringBuilder 字符串变量) 执行速度上 ...

  2. JavaScript历史和标准

    不管新手老手, 学门语言如果不简单了解这门语言谁创立的, 什么时候, 现在由谁来维护, 规范在哪? 总感觉, 少了点什么, 我就是这样. 历史 1994年美国网景(Netscape)公司发布自己的浏览 ...

  3. BioinfomaticsPPT-1-Introduction

    1.人类基因基础  2.基因组规模 3.基因分化表达 4.遗传信息流 5.基因基础知识相关 6.遗传编码 7.序列比对的意义 8.

  4. SQL LEFT JOIN

    SQL LEFT JOIN 关键字 SQL LEFT JOIN 关键字 LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有 ...

  5. 使用Stanford Parser进行句法分析

    一.句法分析 1.定义 句法分析判断输入的单词序列(一般为句子)的构成是否合乎给定的语法,并通过构造句法树来确定句子的结构以及各层次句法成分之间的关系,即确定一个句子中的哪些词构成一个短语,哪些词是动 ...

  6. 剑指offer(第2版)刷题 Python版汇总

    剑指offer面试题内容 第2章 面试需要的基础知识 面试题1:赋值运算符函数 面试题2:实现Singleton模式  解答 面试题3:数组中重复的数字 解答 面试题4:二维数组中的查找 解答 面试题 ...

  7. appium 自动化测试案例

    原文地址http://www.cnblogs.com/tobecrazy/p/4579631.html 原文地址http://www.cnblogs.com/tobecrazy/ 该博主有很多干货,可 ...

  8. beego——错误处理

    beego通过Redirect方法来进行跳转: func (this *AddController) Get() { this.Redirect("/", 302) } 如何终止此 ...

  9. LeetCode:二叉搜索树中的搜索【700】

    LeetCode:二叉搜索树中的搜索[700] 题目描述 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 N ...

  10. HDU4627

    /*找规律,n是奇数那么就是n/2和n/2+1 如果n是偶数,那就是两种情况n/2-1,和n/2-2两种,比较一下大小就可以 思路来自:http://www.cnblogs.com/freezhan/ ...