- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
抱歉这个问题的长度。我是 Java 的新手,我遇到了一些让我很困惑的事情。我是 Java 的新手,我什至还不知道所有的术语,所以请耐心等待;我有大约 3 年的 PHP 经验(主要是过程,不是 OO),但很少有 Java。我也知道使用 System.out.println 进行调试是错误的方法,但它有效,而且我已经习惯了(如果必须的话,在这里插入关于 PHP 程序员的笑话)。我仍在尝试弄清楚如何使用 NetBeans 调试器。
我正在为使用 Struts (1.x) 的网络应用程序添加一项功能。我遇到的问题似乎是一个方法被声明为希望将 String 传递给它,但是对该方法进行反射表示它需要 String[] (一个字符串数组)。我受到限制,因为我无法真正对应用程序进行重大的结构更改,当然我必须确保我不会破坏当前正在运行的应用程序中的任何内容,所以我正在努力使我的已经存在的内容发生变化。所以,对于这个问题...
这里是声明方法的地方(从这些中删除了许多行以仅显示我希望是相关位的内容):
AEReportBean.java:
public class AEReportBean {
private String selectedDownloadFields = null;
public String getSelectedDownloadFields() {
return selectedDownloadFields;
}
// Note that there is no overloading of this function anywhere, this is the only declaration.
public void setSelectedDownloadFields(String selectedDownloadFields) {
this.selectedDownloadFields = selectedDownloadFields;
}
}
当用户点击表单上的“提交”时,AEReportSubmitAction.java 会处理它:
public class AEReportSubmitAction extends BaseAction {
public ActionForward doExecute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response
) throws Exception {
// This works fine, the paramater is getting passed in the request:
System.out.println("URL parameter: " + request.getParameter("selectedDownloadFields");
AEReportBean bean = new AEReportBean(request.getLocale(), 0);
PropertyUtil.setAllFromRequest(request, bean);
// This prints "Null", meaning the setAllFromRequest line above is failing to set this property.
System.out.println("AEReportSubmitAction.java - bean.getSelectedDownloadFields() after setAllFromRequest: " + bean.getSelectedDownloadFields());
}
}
PropertyUtil.setAllFromRequest() 是魔术和真正问题发生的地方:
public class PropertyUtil {
/**
* Takes all the parameters from the request object and if there's a matching
* mutator method in the bean, sets it
*/
static public void setAllFromRequest(ServletRequest request, Object out) {
// Iterate through all the request parameter names and try to set each one.
for (Enumeration parameterNames = request.getParameterNames(); parameterNames.hasMoreElements();) {
String name = (String) parameterNames.nextElement();
try {
PropertyUtil.setSimpleProperty(out, name, request.getParameter(name));
}
catch (Exception e) {
log.info("Exception while setting properties from the Request. parameterName=" + name, e);
}
}
}
/**
* Sets the property from an object using the object's mutator method.
* Assumes naming conventions for accessor methods
* @param bean the object to get the property from
* @param property the name of the property to obtain
* @param newProperty the object to set
*/
// NOTE: This just seems to be a wrapper for the method below it...
static public void setSimpleProperty(Object bean, String property, Object newProperty) throws Exception {
PropertyUtil.setSimpleProperty(bean, property, newProperty, null);
}
/**
* Sets the property from an object using the object's mutator method.
* Assumes naming conventions for accessor methods
* @param bean the object to get the property from
* @param property the name of the property to obtain
* @param newProperty the object to set
*/
static public void setSimpleProperty(Object bean, String property, Object newProperty, Class type) throws Exception {
// Capitalize the first letter in the property and append "set" to the front
String methodName = "set" + property.substring(0, 1).toUpperCase() + property.substring(1);
Method method;
Class[] parameters;
// If the Type was passed in when this method was called, simply add it to the Class array.
if (type != null) {
parameters = new Class[]{type};
}
// If the Type was not specified, determine the Type's class by calling getClass() on it; that class will be used below to call the appropriate setter method.
else {
parameters = new Class[]{newProperty.getClass()};
}
// Here's the reflection problem...
// Iterate through all the methods in the bean. If the method is named "setSelectedDownloadFields", print out some info about it.
for (Method m : bean.getClass().getMethods()) {
if (m.getName().equals("setSelectedDownloadFields")) {
// newProperty is the incoming data that ultimately comes from the HTML form field.
System.out.println("newProperty.getClass(): " + newProperty.getClass()); // Prints "class java.lang.String"
// Added for Cameron Skinner in comments.
System.out.println("m.toGenericString: " + m.toGenericString()); // Prints "public void com.[company deleted].bean.AEReportBean.setSelectedDownloadFields(java.lang.String[])"
System.out.println("m.getName(): " + m.getName()); // Prints "setSelectedDownloadFields"
System.out.println("parameters:");
for (Class c : m.getParameterTypes()) {
System.out.println("--c.getCanonicalName(): " + c.getCanonicalName()); // Prints "java.lang.String[]"
System.out.println("--c.getName(): " + c.getName()); // Prints "[Ljava.lang.String;"
}
}
}
// And here's where it fails...
try {
System.out.println("bean.getClass(): " + bean.getClass()); // Prints "class com.[company deleted].bean.AEReportBean"
System.out.println("methodName: " + methodName); // Prints "setSelectedDownloadFields"
System.out.println("for (Class p : parameters):");
for (Class p : parameters) {
System.out.println("--p.getCanonicalName(): " + p.getCanonicalName()); // Prints "java.lang.String"
}
// Here it looks for a method called, effectively, AEReportBean.setSelectedDownloadFields(String s), but above we see that reflection is showing it as AEReportBean.setSelectedDownloadFields(String[] s), so the try block fails.
method = bean.getClass().getMethod(methodName, parameters);
}
catch (NoSuchMethodException e) {
// All lines below here also fail until it bombs out with the exception at the bottom...
// If no method can be found, then see if it's a primitive type that
// has been wrapped
Class valueClass = newProperty.getClass();
//System.out.println("valueClass.toString() = " + valueClass.toString());
try {
if (valueClass.equals(Integer.class)) {
method = bean.getClass().getMethod(methodName, new Class[]{int.class});
}
else if (valueClass.equals(Double.class)) {
method = bean.getClass().getMethod(methodName, new Class[]{double.class});
}
else if (valueClass.equals(Long.class)) {
method = bean.getClass().getMethod(methodName, new Class[]{long.class});
}
else if (valueClass.equals(Float.class)) {
method = bean.getClass().getMethod(methodName, new Class[]{float.class});
}
else {
throw new Exception(e.getMessage());
}
}
catch (NoSuchMethodException ex) {
throw new Exception(ex.getMessage());
}
}
// If it had gotten to this point, it would call the method with the appropriate parameters, and the property would be set.
try {
// Now execute the method
method.invoke(bean, new Object[]{newProperty});
}
catch (Exception ex) {
throw new Exception(ex.getMessage());
}
}
}
我真的不知道我在这里错过了什么,但肯定有一些东西。同一页面上的其他 HTML 表单元素可以完美工作。如果需要更多信息,请告诉我。谢谢!
最佳答案
代码结果不会说谎。它基本上是在说明类(class)不是您期望的那样。您的项目的类路径中有多个不同版本的 AEReportBean
类,可能在不同的包中,并且导入了错误的类或在类加载中获得了优先权。在 Netbeans 中执行类型/类搜索以在类路径中按给定名称查找所有类(我不使用 Netbeans,但在 Eclispe 中它是 Ctrl+Shift+T,Netbeans 等效项可能是 Alt+Shift+O )。
更新:另一个可能的原因是 Netbeans 没有在保存源文件时自动构建项目(IDE 应该在构建期间创建/刷新 .class
文件).查看设置中的某处。
关于Java 反射不同意方法声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4494988/
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 8年前关闭。 Improve this q
我编写了简单的 Java 应用程序供自己使用,它扫描 Excel 文档并从中提取一些事件。对于下一阶段,我希望它登录我的 Google 帐户并使用 Google Calendar API 创建这些事件
我有一个带有链接标记的复选框。单击链接标签时,我的模式打开。单击按钮然后选中复选框时我有模式 I agree with "Terms a
Consent SDK允许显示同意书,但目前只有英文版本(SDK 版本 1.0.3)。 SDK页面说: To update consent text of the Google-rendered co
您好,我需要一些关于如何应用丝潮cookie同意脚本的帮助http://silktide.com/cookieconsent/code 。 (由于欧盟 cookie 法等原因,我想对此进行广告)。现在
我收到了一封来自 Goolge 的关于在我的网站上实现 cookie 许可的电子邮件。我在 Play 商店中只发布了一个 Android 应用程序。它使用广告获利。有必要实现 cookie 同意消息吗
我正在尝试从 Youtube channel 检索 Youtube 视频列表,例如“https://www.youtube.com/user/YouTube/videos”,以获取第 n 个视频(感谢
我想抓取(使用 rvest )一个要求用户同意设置 cookie 的网站。如果我只是抓取页面,则 rvest 只会下载弹出窗口。这是代码: library(rvest) content % html_
我有一个 HTML 网站,我想向其中添加 Cookie-Consent。 类似于 Stackoverflow:Stackoverflow Screenshot 将会出现可能包含屏幕截图中所示链接的文本
我正在尝试将字符串与字符串向量匹配: a <- c('abcde', 'abcdf', 'abcdg') agrep('abcdh', a, max.distance=list(substitutio
我目前正在尝试集成 Google Consent SDK。onConsentInfoUpdated(consentStatus:ConsentStatus) 函数发生了一些我无法理解的事情 用户第一次
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭去年。 Im
我们在 Azure AD 中有一个使用 Microsoft Graph API 的应用。该应用程序使用 4 个基本权限成功运行:电子邮件、个人资料 User.Read User.ReadBasic.A
我们在 Azure AD 中有一个使用 Microsoft Graph API 的应用。该应用程序使用 4 个基本权限成功运行:电子邮件、个人资料 User.Read User.ReadBasic.A
此代码中未使用变量“status”。因此我无法检查用户的同意选择。 我必须将“状态数组”存储在默认值中。一旦当前状态在数组中可用,它将不会显示同意书。如果没有,那么我们将显示表单并将该状态添加到默认状
这是一个例子: 具体的实现可能有一些特定的行为,比如 ArrayList 的实现有 ensureCapacity(),它不适用于 LinkedList。 //programming to interf
我在一个网站上工作,我们目前有一个 cookie 横幅,它在启动时显示并有一个确定按钮。然而,我想改变这一点,让用户可以选择打开/关闭他们希望被跟踪的 cookie 类别。像这样: 我是 reactj
我正在使用 Mac OS X 10.9.5 和 git 版本 2.1.2。进入 推我收到: 同意 Xcode/iOS 许可证需要管理员权限,请通过 sudo 以 root 身份重新运行。 致命:无法从
搜索似乎只能找到使用 JavaScript 的解决方案。我想显示一个 Cookie 同意消息,即使访问者使用脚本拦截器也能看到它,所以我想知道这是否可以使用 CSS 和 HTML 来完成,而不需要任何
fun sample(list_of_str_lists, s) = case list_of_str_lists of [] => [] | x::[]
我是一名优秀的程序员,十分优秀!