struts2注入类
struts2是能够注入一个对象的,那么一定须要继承ModelDriven的泛型接口。
package com.test.action; import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.test.model.Contact;
import com.test.model.User; public class ValideAction extends ActionSupport implements ModelDriven<User>{ /**
*
*/
private static final long serialVersionUID = 1L; private User user;
private String username;
private Contact contact; public Contact getContact() {
return contact;
} public void setContact(Contact contact) {
this.contact = contact;
} 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;
} private String password; public String execute()
{
System.out.println(user.getContact().getPhone());
return SUCCESS;
} @Override
public User getModel() {
// TODO Auto-generated method stub
if (null == user) {
return user = new User();
}
return user;
} }
如上面的代码所看到的,就是会将User注入到类action中。而User也是一个嵌套类。
User
package com.test.model;
public class User {
private String username;
private Contact contact;
public Contact getContact() {
return contact;
}
public void setContact(Contact contact) {
this.contact = contact;
}
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;
}
private String password;
public String toString()
{
return username+" "+password;
}
}
里面包括一个联系信息类Contact
package com.test.model;
public class Contact {
private String phone;
private String address;
private String email;
public Contact()
{
}
public Contact(String phone,String address,String email)
{
this.phone=phone;
this.address=address;
this.email=email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
然后JSP页面的input类型例如以下所看到的
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 这个地方是用来显示错误信息 -->
<s:fielderror/>
<form action="data.action" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
-------------------联系方式----------------<br>
手机:<input type="text" name="contact.phone"><br>
地址:<input type="text" name="contact.address"><br>
邮箱:<input type="text" name="contact.email"><br> <input type="submit" name="ok"><br>
</form>
</body>
</html>
输出JSP页面例如以下所看到的
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
測试结果页面<br>
用户名:<s:property value="username"/><br>
密码:<s:property value="password"/><br>
--------------------联系方式-------------<br>
手机:<s:property value="contact.phone"/><br>
地址:<s:property value="contact.address"/><br>
邮箱:<s:property value="contact.email"/><br> <s:debug></s:debug>
</body>
</html>
终于的输出结果如图所看到的
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaXRidWx1b2dl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
从这个样例中。学会了跳跃性的解决这个问题的思路。另外就是对依赖注入还不是很了解,这个须要重点去学习和理解一下,特别是经常使用的几种注入方式。
struts2注入类的更多相关文章
- 简单实用的PHP防注入类实例
这篇文章主要介绍了简单实用的PHP防注入类实例,以两个简单的防注入类为例介绍了PHP防注入的原理与技巧,对网站安全建设来说非常具有实用价值,需要的朋友可以参考下 本文实例讲述了简单实用的PHP防注 ...
- springboot管理类,springboot注入类
springboot管理类,springboot注入类 定义一个配置类,添加@Configuration注解,EvaluatorTemplate代表你需要注入的第三方类 @Configuration ...
- springmvc注入类 NoUniqueBeanDefinitionException: No qualifying bean of type [] is defined: expected single错误
在springmvc中注入服务时用@Service 当有两个实现类时都标明@Service后则会出现异常: nested exception is org.springframework.beans. ...
- Spring_02 注入类型值、利用引用注入类型值、spring表达式、与类相关的注解、与依赖注入相关的注解、注解扫描
注意:注入基本类型值在本质上就是依赖注入,而且是利用的set方式进行的依赖注入 1 注入基本类型的值 <property name="基本类型的成员变量名" value=&q ...
- spring类扫描注入-----类扫描的注解解析器
通过类扫描注入到容器中,这种方式,在实际开发中还是很常用的,可以看下自己的配置文件,就会发现,自己公司的项目,搞不好就是这么注入的. 起码,我发现我公司的项目就是这么干的. 下面来演示一下简单的例子: ...
- c#.net全站防止SQL注入类的代码
using System;using System.Collections.Generic;using System.Linq;using System.Web; /// <summary> ...
- JAVA框架Struts2 Action类
一.Action书写方式: 接口地址:https://struts.apache.org/maven/struts2-core/apidocs/index.html Action类就是一个POJO类. ...
- .net 过滤 sql防注入类,省地以后每次都要重新弄!
/// <summary> /// 过滤不安全的字符串 /// </summary> /// <param name="Str"&g ...
- Struts2 Action类的创建以及参数传递以及接收
一.Struts中Action得创建方式 1,直接创建一个简单的Action类 添加Struts.xml,配置转发方法返回转发的页面. 2,实现一个Action类 Strust.xml配置对应的Url ...
随机推荐
- JumpServer 堡垒机 指南
堡垒机介绍 在一个特定网络环境下,为了保障网络和数据不受外界入侵和破坏,而运用各种技术手段实时收集和监控网络环境中每一个组成部分的系统状态.安全事件.网络活动,以便集中报警.及时处理及审计定责. ...
- Inversion of Control Containers and the Dependency Injection pattern--Martin Fowler
原文地址:https://martinfowler.com/articles/injection.html n the Java community there's been a rush of li ...
- Linux下VsFTP和ProFTP用户管理高级技巧 之一
Linux下VsFTP和ProFTP用户管理高级技巧 FTP服务时互联网上比较古老的一种应用,至今Interner应用面非常广泛,但令管理员头痛不已的是其用户管理,既多且杂,如何解决这一问 ...
- unalias---取消命令别名
unalias命令用来取消命令别名,是为shell内建命令. 选项 -a:取消所有命令别名. 实例 使用unalias命令将已经设置的命令别名"cc"取消,输入如下命令: unal ...
- sleep---暂停指定的时间
sleep命令可以用来将目前动作延迟一段时间. 使用权限:所有使用者. 语法 sleep [--help] [--version] number[smhd] 参数说明: --help : 显示辅助讯息 ...
- ZOJ 3587 Marlon's String 扩展KMP
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3587 题意:给出两个字符串S和T.S,T<=100000.拿出 ...
- PHP 版本简单记录
PHP 版本简单记录 PHP 博物馆 http://museum.php.net/php5/ PHP 版本发布 https://secure.php.net/release ...
- 32.Node.js中的常用工具类util
转自:http://www.runoob.com/nodejs/nodejs-module-system.html util是一个Node.js核心模块,提供常用函数的集合,用于弥补JavaScrip ...
- Hp Open View安装使用视频
去年完成的cisco works 2000操作(http://chenguang.blog.51cto.com/blog/350944/124729)视频深受广大博友欢迎许多人来信咨询,最近刚做完一个 ...
- position(static-relative-absolute-fixed),margin(top-right-bottom-left),top-right-bottom-left
最近写css遇到一些问题,所以准备写下来捋一下思路. 1.position=satic下看margin的使用.(top-right-bottom-left在这种case下无效) 1-1)margin ...