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. mysql模糊查询实践总结

    %代表任意多个字符 _代表一个字符 在 MySQL中,SQL的模式缺省是忽略大小写的 正则模式使用REGEXP和NOT REGEXP操作符. “.”匹配任何单个的字符.一个字符类 “[...]”匹配在 ...

  2. xxxservlet继承HttpServlet类

    "HttpServlet类被定义为抽象类,但是源码里面没有抽象方法.所以没有一定要求实现的方法.之所以定义为抽象类,是因为他继承了GenericServlet这个抽象类.并没有全部实现里面的 ...

  3. Angular学习笔记—架构简述

    这个架构图展现了 Angular 应用中的 8 个主要构造块: 模块 (module) 组件 (component) 模板 (template) 元数据 (metadata) 数据绑定 (data b ...

  4. Java基础—枚举

    定义 枚举(enum)类型是Java 5新增的特性,它是一种新的类型,允许用常量来表示特定的数据片断,而且全部都以类型安全的形式来表示. 为什么要用枚举 在java语言中还没有引入枚举类型之前,表示枚 ...

  5. 113. Path Sum II(求等于某个数的所有路径)

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  6. c#的yield return

    4.1 迭代器块 一个迭代器块(iterator block)是一个能够产生有序的值序列的块.迭代器块和普通语句块的区别就是其中出现的一个或多个yield语句. yield return语句产生迭代的 ...

  7. 什么是jstack

    以下是百度百科的内容 jstack是java虚拟机自带的一种堆栈跟踪工具. jstack用于生成java虚拟机当前时刻的线程快照.线程快照是当前java虚拟机内每一条线程正在执行的方法堆栈的集合,生成 ...

  8. SET 语句积累

    SET IDENTITY_INSERT 表名字 off 注解: 把值插入到自动编号(或者说是标识列,IDENTITY)中去,需要设定 SET IDENTITY_INSERT 语法:SET IDENTI ...

  9. 【Head First Servlets and JSP】笔记 25:JSTL 参考

    <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ t ...

  10. 20145222 黄亚奇 《网络对抗》Exp8 Web基础

    20145222 黄亚奇 <网络对抗>Exp8 Web基础 实践具体要求 (1).Web前端HTML(1分) 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法 ...