- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个使用 Servlets
的应用程序。我被要求不要使用Struts2 or JSF or SpringMVC
由我的老板但仅限 Servlets and Jsps
。在我的Servlet
我需要调用request.getParameter("parameterName")
检索参数值。我的问题是否有一个 Java 库可供我传递 JavaBean
和HttpServletRequest
对象并返回填充的 JavaBean
。即 JavaBean
自动填充请求参数。
最佳答案
我通常使用任何类型的 JavaBean 反射内容来访问 Commons BeanUtils果然他们在 BeanUtilsBean
中有一个合适的方法:
public void populate(Object bean,
Map<String,? extends Object> properties)
throws IllegalAccessException,
InvocationTargetExceptionPopulate 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/
这两个人似乎在做同样的事情。谁能解释两者之间的主要区别?你什么时候使用一个与另一个? HttpServletRequest.getRemoteUser() HttpServletRequest.get
我现在尝试了很多东西,但我似乎错过了一 block 拼图。这是故事:我有一个请求范围的 bean,它从 HttpServletRequest 读取一些 SessionContext。此属性在过滤器中设
使用HttpServletRequest.login(String, String)登录后,使用以下代码,在以下请求中,我仍然会收到基本身份验证提示。为什么login函数在我的配置中不起作用? 我的终
我为 HttpservletRequest 设置路径信息,如下所示。 request.setAttribute("javax.servlet.include.path_info", pathInfo)
我在表单和请求方面遇到了这个问题。我正在使用 sencha 和 javascript 创建一个网页,该网页将表单发布到 java Web 应用程序,该应用程序从数据库中提取数据并对其进行格式化,然后再
我曾经在 JSF 2 应用程序中基于 cookie 的对话过滤器中打开 session 。现在我想建立相同的机制,但与技术无关。重用一些代码,我将其编写在扩展 OncePerRequestFilter
我从我的客户端向我的服务器发送一个 ajax 请求。 这是我传递的数据结构: data = {"key1" : "value1" , "key2" : {"subkey1": "subvalue1"
如何使用 HttpServletRequest 中的信息识别集群中的节点? 每个节点唯一的任何信息都是合适的 - 我需要它来区分日志。 最佳答案 你可以尝试获取IP和hostname // Get c
我过滤我的 REST 服务的 BasicAuth 并将用户名存储在 HttpServletRequest 中 AuthFilter相关代码 public class AuthFilter implem
我想实现一个速度工具,它提供了一种方法来查明用户是否登录。我正在使用 VelocityLayoutServlet 以便在每个请求上呈现模板。 我的 velocity-tools.xml 看起来像这样:
我正在 Spring MVC 上 Swagger 玩,运行本地 jetty ,由于某种原因得到一个 null HttpServletRequest,至少可以说这很奇怪。 这是完整的堆栈跟踪: java
Class Permission implements ContainerRequestContext { @context HttpServletRequest servletReq
我在启用双向 SSL 的 Weblogic 10.3 中运行一个 servlet。当我尝试从 HTTPServletRequest 获取以下属性时,它们都是空的: - javax.servlet.re
我已经导入了以下内容导入 javax.servlet.http.*; 我想获取首选语言的浏览器 HttpServletRequest request = ServletActionContext.ge
我想拦截过滤器/servlet 中的请求并向其添加一些参数。但是,该请求不会公开“setParameter”方法,并且在操作参数映射时会抛出一个错误,说明它已被锁定。有没有我可以尝试的替代方案? 最佳
为了测试,我想从一些预定义的数据构建 HttpServletRequest 对象,例如: GET / HTTP/1.1 User-Agent: Opera/9.80 (Windows NT 6.1;
在我的 servlet 中,req.getQueryString() 在向其发送 ajax 请求时返回 null。这是因为 req.getQueryString() 只适用于 GET 而不是 POST
我使用打印编写器直接在 servlet 中打印列表,然后打印列表。 当我尝试放入 jsp 时,列表不会打印我使用的是 JSTL 还是 scriptlet。 我尝试在 JSTL 和 scriptlet
我有一个 jsp,其中包含一个 jquery post 到我的 tomcat 服务器上的一个 servlet,它创建了一个 HttpServletRequest。我想确保只处理我的 jsp 对我的 s
我对HttpServletRequest生命对象有疑问。 request对象进入controller后是否被销毁? 最佳答案 HttpServletRequest 对象的生命周期就是:为 HTTP S
我是一名优秀的程序员,十分优秀!