Struts03---参数传递
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---参数传递的更多相关文章
- js学习之函数的参数传递
我们都知道在 ECMAScript 中,数据类型分为原始类型(又称值类型/基本类型)和引用类型(又称对象类型):这里我将按照这两种类型分别对函数进行传参,看一下到底发生了什么. 参数的理解 首先,我们 ...
- kettle中含有参数传递的定时任务
(1)新建一个作业(新建->作业),并在控制面板右键: (2)设置一个命令参数: (3)把作业的参数传递给转换: (4)在转换中右键设置转换属性: (5)接收作业中设置的传递参数: (6)参数的 ...
- Java基础知识笔记(七:接口、变量作用域和参数传递)
一.接口 Java语言不允许一个子类拥有多个直接父类,即任何子类只能有一个直接父类.但允许一个类实现多个接口,即在定义类的接口名称列表中可以包含1个或多个接口名称,从而实现多重继承的特性.接口的定义格 ...
- shell 脚本之获取命令输出字符串以及函数参数传递
在ubuntu 14.04之后,所有的U盘挂载也分用户之分,最近很多操作也和U盘有关,所以就研究了一上午shell脚本函数以及字符串操作的方法. 字符串操作: 获取他的命令输出比较简单,打个简单的比方 ...
- 【GoLang】golang 闭包 closure 参数传递的蹊跷!
结论: 闭包函数可以直接引用外层代码定义的变量, 但是,注意,闭包函数里面引用的是变量的地址, 当goroutine被调度时,改地址的值才会被传递给goroutine 函数. 介绍 go的闭包是一个很 ...
- JQuery Mobile 页面参数传递
在单页模版中使用基于HTTP的方式通过POST和GET请求传递参数,而在多页模版中不需要与服务器进行通信,通常在多页模版中有以下三种方法来实现页面间的参数传递. 1.GET方式:在前一个页面生成参数并 ...
- Java 中的值传递和参数传递
Java中没有指针,所以也没有引用传递了,仅仅有值传递不过可以通过对象的方式来实现引用传递 类似java没有多继承 但可以用多次implements 接口实现多继承的功能 值传递:方法调用时,实际参数 ...
- 【Python学习】函数参数传递方法四种(位置,关键字,默认值,包裹位置,包裹关键字传递)
1. 位置传递: #--coding:utf-8-- def send(name,address): return 'package is sent to %s, located in %s' %(n ...
- JAVA反射参数传递
引用:http://fish2700.blog.163.com/blog/static/130713192009103035723281/ 使用Method反射调用函数时,我们通常会遇到以下几种情况: ...
- [蟒蛇菜谱]Python函数参数传递最佳实践
将函数作为参数传递,同时将该函数需要的参数一起传递.可参考threading.Timer的处理方式: class threading.Timer(interval, function, args=[] ...
随机推荐
- window7+wamp环境配置Oracle数据库连接
最近开发需要使用的oracle数据库!翻看了PHP手册,也在网上找了些帖子!功夫不负有心人,花费了四五个小时的时间,终于找到了Oracle的配置方法.下面就讲解下如何配置Oracle数据库连接吧! 附 ...
- Xamrin开发安卓笔记(一)
http://www.cnblogs.com/minCS/p/4108133.html Xamrin开发安卓笔记(一) 安装篇 环境虽然搭建的不稳定,不过还是可以开发的,又加了两个环境变量不知道有 ...
- 从零到一创建ionic移动app:应用anjularjs编写ionic项目
推荐两篇文章,带你入门 ionic中文项目(作为了解ionic基础结构用):http://blog.csdn.net/i348018533/article/details/47258449/ ioni ...
- Lambda加自定义比较器实现两个列表的合并
一次项目有这样的需求,本地存储了json数据,可以转化为对应的List列表,现在需要更新,从服务器那里获取最新的数据更改.总的来说就是本地有个List表,如果数据需要更新,则会向服务器发送请求来获取需 ...
- phpword使用
composer 安装 https://packagist.org/packages/phpoffice/phpword 开发文档:http://phpword.readthedocs.io/en/l ...
- CF335B
/*CF335B 这个题目的n达到50000,但是串只是有小写字母组成,所以如果字符串的长度大于2600,那么 肯定存在,所开始输入就判断如果长度大于2600,那么直接找当个字母输出100个 否则执行 ...
- minicom退出方法【转】
本文转载自:https://blog.csdn.net/jhyworkspace/article/details/53572284 1)需使用Ctrl+a 进入设置状态2)按z进入设置菜单(1)S键: ...
- 解决Vim插入模式下backspace按键无法删除字符的问题【转】
本文转载自:https://blog.csdn.net/zxy987872674/article/details/64124959 最近使用某个服务器编辑文件时,快捷键i进入插入模式后,下方不出现in ...
- Linux 利用管道父子进程间传递数据
[原文] fork()函数:用于创建子进程,子进程完全复制父进程的资源,相当于父进程的拷贝.具体理解,运用父进程的同一套代码,通过判断进程ID来执行不同进程的不同任务. 返回值正常为子进程ID,出错返 ...
- 未来简史之数据主义(Dataism)
https://www.jianshu.com/p/8147239c9cb0?from=singlemessage junjguo 关注 2017.04.24 22:08* 字数 8116 阅读 31 ...