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=[] ...
随机推荐
- 三道半平面交测模板题 Poj1474 Poj 3335 Poj 3130
求半平面交的算法是zzy大神的排序增量法. ///Poj 1474 #include <cmath> #include <algorithm> #include <cst ...
- python进阶——进程/线程/协程
1 python线程 python中Threading模块用于提供线程相关的操作,线程是应用程序中执行的最小单元. #!/usr/bin/env python # -*- coding:utf-8 - ...
- CROS跨域 解决方案 之 tomcat 做过滤处理解决
摘自:http://www.cnblogs.com/liuwenhao-1/articles/6963540.html 1 .在项目中常常遇到本地访问服务器上的链接数据访问不到,并出现如下问题: 这是 ...
- cocos2d-x与着色器设计--入门篇(游云凌天原创)
http://blog.csdn.net/danjinxiangsi/article/details/43949955 着色器(Shader)应用与计算机图形学领域,指一组提供计算机图形资源在渲染时执 ...
- python之模块导入和重载
模块导入和重载 模块导入通过import语句实现,但是在同一次会话中只运行一次. 若想要再次运行文件,调用imp标准库中的reload函数: >>> from imp import ...
- 求最长不下降子序列(nlogn)
最长递增子序列问题:在一列数中寻找一些数,这些数满足:任意两个数a[i]和a[j],若i<j,必有a[i]<a[j],这样最长的子序列称为最长递增子序列. 设dp[i]表示以i为结尾的最长 ...
- Spyder如何在弹出框绘图【转】
本文转载自:https://blog.csdn.net/weixin_39231685/article/details/81028833 Spyder绘图默认出现在console面板,图片无法放大,看 ...
- CVE补丁安全漏洞【学习笔记】
更新安卓系统的CVE补丁网站:https://www.cvedetails.com/vulnerability-list/vendor_id-1224/product_id-19997/version ...
- Windows7中pagefil.sys和Hiberfil.sys文件删除与转移
第一步.在开始的功能表的搜索栏里输入 cmd,然后在搜索结果中的 cmd 按下右键,点选[以系统管理员身分运行] 第二步.在命令提示符窗口里输入下面命令然后按下Enter: powercfg –h o ...
- Hibernate缓存何时使用和如何使用
http://developer.51cto.com/art/201202/315922.htm 1. 关于hibernate缓存的问题: 1.1. 基本的缓存原理 Hibernate缓存分为二级, ...