首页 > 代码库 > Java 反射 应用
Java 反射 应用
利用反射 构造get set 方法:
1 package com.wages.salary.service.common; 2 3 import java.lang.reflect.InvocationTargetException; 4 import java.lang.reflect.Method; 5 6 import com.wages.salary.beans.Salary; 7 8 public class BaseReflect { 9 public static void setValue(Salary s, String value, String itemName) {10 String methodName = "set" + itemName.charAt(0) + itemName.substring(1).toLowerCase();11 try {12 Method m = s.getClass().getMethod(methodName, String.class);13 m.invoke(s, value);14 } catch (NoSuchMethodException | SecurityException e) {15 e.printStackTrace();16 } catch (IllegalAccessException17 | IllegalArgumentException18 | InvocationTargetException e) {19 e.printStackTrace();20 }21 }22 23 public static String getValue(Salary s, String itemName) {24 String returnValue = http://www.mamicode.com/null;25 String methodName1 = "get" + itemName.charAt(0) + itemName.substring(1).toLowerCase();26 try {27 Method m1 = s.getClass().getMethod(methodName1, null);28 returnValue = http://www.mamicode.com/(String) m1.invoke(s,null);29 } catch (NoSuchMethodException | SecurityException e) {30 e.printStackTrace();31 } catch (IllegalAccessException32 | IllegalArgumentException33 | InvocationTargetException e) {34 e.printStackTrace();35 }36 return returnValue;37 }38 39 }
1 <%@page 2 import="com.wages.salary.beans.*,com.wages.accountmanage.beans.Employee,java.util.*,com.wages.salary.service.common.*"%> 3 <%@ page language="java" contentType="text/html; charset=GBK" 4 pageEncoding="GBK"%> 5 <%@taglib uri="/struts-tags" prefix="s"%> 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 7 <html> 8 <head> 9 <meta http-equiv="Content-Type" content="text/html; charset=GBK"> 10 <title>Insert title here</title> 11 <style> 12 #myselect { 13 14 } 15 16 td { 17 text-align: center; 18 border-right: 1px solid #C1DAD7; 19 border-bottom: 1px solid #C1DAD7; 20 } 21 22 th { 23 font: bold 14px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif; 24 color: #4f6b72; 25 border-right: 1px solid #C1DAD7; 26 border-bottom: 1px solid #C1DAD7; 27 border-top: 1px solid #C1DAD7; 28 letter-spacing: 2px; 29 text-transform: uppercase; 30 text-align: center; 31 padding: 16px 6px 16px 12px; 32 background: #CAE8EA no-repeat; 33 } 34 </style> 35 </head> 36 <body> 37 <%-- <%@ include file="formshowandhidden.jsp" %> --%> 38 39 <jsp:include page="formshowandhidden.jsp" /> 40 41 <form action="" method="post"> 42 <table align=center width=750 cellspacing="0" cellpadding="6"> 43 44 45 46 <% 47 List<Item> items = (List<Item>) session.getAttribute("items");
List<Salary> salist = (List<Salary>) session.getAttribute("salarys"); 50 List<Employee> emplist = (List<Employee>) session.getAttribute("employs");
56 if (items.size() <= 0 || items == null) { 57 %> 58 <tr align=center> 59 <th></th> 60 61 </tr> 62 <tr> 63 <td colspan="6">No data!</td> 64 </tr> 65 66 <% 67 } else { 68 %> 69 70 <tr align=center> 71 <th>工号</th> 72 <th>姓名</th> 73 <s:iterator value="#session.items"> 74 <th><s:property value="itemvalue" /></th> 75 </s:iterator> 76 </tr> 77 78 79 <% 80 for (int i = 0; i < salist.size(); i++) { 81 %> 82 <tr> 83 <td><%=emplist.get(i).getEmployeeid()%></td> 84 <td><%=emplist.get(i).getEmployeename()%></td> 85 86 <% 87 for (int t = 0; t < items.size(); t++) { 88 String itemName = items.get(t).getItemkey(); 89 %> 90 <% 91 DESEcrption desPlus = new DESEcrption(); 92 %> 93 <td><%=desPlus.decrypt(BaseReflect.getValue( 94 salist.get(i), itemName))%></td> 95 <%-- <td><%=new String( MCrypt.decrypt( BaseReflect.getValue(salist.get(i), itemName) ))%></td> --%> 96 <% 97 } 98 %> 99 </tr>100 <%101 }102 }103 %>104 105 106 107 </table>108 </form>109 </body>110 </html>
Java 反射 应用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。