gpt4 book ai didi

用于检索 HttpServletRequest 参数的 Java 库

转载 作者:行者123 更新时间:2023-12-01 13:15:36 25 4
gpt4 key购买 nike

我正在开发一个使用 Servlets 的应用程序。我被要求不要使用Struts2 or JSF or SpringMVC由我的老板但仅限 Servlets and Jsps 。在我的Servlet我需要调用request.getParameter("parameterName")检索参数值。我的问题是否有一个 Java 库可供我传递 JavaBeanHttpServletRequest对象并返回填充的 JavaBean 。即 JavaBean自动填充请求参数。

最佳答案

我通常使用任何类型的 JavaBean 反射内容来访问 Commons BeanUtils果然他们在 BeanUtilsBean 中有一个合适的方法:

public void populate(Object bean,
Map<String,? extends Object> properties)
throws IllegalAccessException,
InvocationTargetException

Populate the JavaBeans properties of the specified bean, based on the specified name/value pairs. This method uses Java reflection APIs to identify corresponding "property setter" method names, and deals with setter arguments of type String, boolean, int, long, float, and double. In addition, array setters for these types (or the corresponding primitive types) can also be identified.

The particular setter method to be called for each property is determined using the usual JavaBeans introspection mechanisms. Thus, you may identify custom setter methods using a BeanInfo class that is associated with the class of the bean itself. If no such BeanInfo class is available, the standard method name conversion ("set" plus the capitalized name of the property in question) is used.

NOTE: It is contrary to the JavaBeans Specification to have more than one setter method (with different argument signatures) for the same property.

WARNING - The logic of this method is customized for extracting String-based request parameters from an HTTP request. It is probably not what you want for general property copying with type conversion. For that purpose, check out the copyProperties() method instead.

请注意,它是为从 HTTP 请求中提取基于字符串的请求参数而定制的,因此它正是您想要的。问题是 HTTP 请求参数以 Map<String, String[]> 的形式出现。需要从数组中解开。

我做了一个快速测试以确保它按预期工作。我注意到该类必须public并在其自己的文件中进行内省(introspection)。

我像这样创建一个 JavaBean:

public class JavaBean {

String userName;
String password;
Integer id;

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;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

}

并使用以下内容进行快速测试:

public static void main(String[] args) throws Exception {
final Map<String, String[]> params = new HashMap<>();
params.put("userName", new String[]{"userA"});
params.put("password", new String[]{"secrect"});
params.put("id", new String[]{"10"});
final JavaBean javaBean = new JavaBean();
BeanUtilsBean.getInstance().populate(javaBean, params);
System.out.println(javaBean.getUserName());
System.out.println(javaBean.getPassword());
System.out.println(javaBean.getId());

}

输出符合预期:

userA
secrect
10

我还注意到它似乎不喜欢 int对于数字属性 - 似乎期望 Integer .

关于用于检索 HttpServletRequest 参数的 Java 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22500447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com