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注入类的更多相关文章

  1. 简单实用的PHP防注入类实例

    这篇文章主要介绍了简单实用的PHP防注入类实例,以两个简单的防注入类为例介绍了PHP防注入的原理与技巧,对网站安全建设来说非常具有实用价值,需要的朋友可以参考下   本文实例讲述了简单实用的PHP防注 ...

  2. springboot管理类,springboot注入类

    springboot管理类,springboot注入类 定义一个配置类,添加@Configuration注解,EvaluatorTemplate代表你需要注入的第三方类 @Configuration ...

  3. springmvc注入类 NoUniqueBeanDefinitionException: No qualifying bean of type [] is defined: expected single错误

    在springmvc中注入服务时用@Service 当有两个实现类时都标明@Service后则会出现异常: nested exception is org.springframework.beans. ...

  4. Spring_02 注入类型值、利用引用注入类型值、spring表达式、与类相关的注解、与依赖注入相关的注解、注解扫描

    注意:注入基本类型值在本质上就是依赖注入,而且是利用的set方式进行的依赖注入 1 注入基本类型的值 <property name="基本类型的成员变量名" value=&q ...

  5. spring类扫描注入-----类扫描的注解解析器

    通过类扫描注入到容器中,这种方式,在实际开发中还是很常用的,可以看下自己的配置文件,就会发现,自己公司的项目,搞不好就是这么注入的. 起码,我发现我公司的项目就是这么干的. 下面来演示一下简单的例子: ...

  6. c#.net全站防止SQL注入类的代码

    using System;using System.Collections.Generic;using System.Linq;using System.Web; /// <summary> ...

  7. JAVA框架Struts2 Action类

    一.Action书写方式: 接口地址:https://struts.apache.org/maven/struts2-core/apidocs/index.html Action类就是一个POJO类. ...

  8. .net 过滤 sql防注入类,省地以后每次都要重新弄!

    /// <summary>    /// 过滤不安全的字符串    /// </summary>    /// <param name="Str"&g ...

  9. Struts2 Action类的创建以及参数传递以及接收

    一.Struts中Action得创建方式 1,直接创建一个简单的Action类 添加Struts.xml,配置转发方法返回转发的页面. 2,实现一个Action类 Strust.xml配置对应的Url ...

随机推荐

  1. vue的指令在webstrom下报错

    Preferences -> Editor -> Inspections找到XML,把 Unbound XML namespace prefix的勾去掉

  2. 配置Lotus Domino集群视频详解

    IBM Lotus Domino Server 有个最重要的高可用特新就是集群,它对于任何使用 Domino 服务器的组织,让用户能够持续访问它们的数据库,保证业务不间断运行.下面通过两个视频来学习如 ...

  3. nginx最新配置

    #user  nobody;worker_processes  1; #error_log  logs/error.log;#error_log  logs/error.log  notice;#er ...

  4. linux 批量设置文件夹755 文件644权限

    linux 批量设置文件夹755 文件644权限 文件来源 http://www.111cn.net/sys/linux/109724.htm 本文章来为各位介绍一篇关于linux 批量设置文件夹75 ...

  5. TrueSec引导的Linux系统和安全检测工具预览

      650) this.width=650;" onclick='window.open("http://blog.51cto.com/viewpic.php?refimg=&qu ...

  6. RecyclerView 展示多种类型Item数据

    一.多Item布局实现(MultipleItem) 如果之前你用过ListView实现过此功能,那么你一定对下面这两个方法并不陌生 @Override public int getItemViewTy ...

  7. Android 多线程下载,断点续传,线程池

    你可以在这里看到这个demo的源码: https://github.com/onlynight/MultiThreadDownloader 效果图 这张效果图是同时开启三个下载任务,限制下载线程数量的 ...

  8. 福建省第八届 Triangles

    Problem Description This is a simple problem. Given two triangles A and B, you should determine they ...

  9. Linux shell command学习笔记(二)

    <cut> 作用:从输入文件或者命令的输出中析取出各种域 用法:cut –c{字符串范围} –d{字段间分割符} –f{字段索引编号} 举例: (1)查看在线用户:who | cut –c ...

  10. vue中v-for的用法以及参数的作用

    <template> <div> <div class="content clearfix" v-on:click="goorderingD ...