- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在通过 maven 运行 JUnit 测试,其中正在测试进行以下调用的 struts 操作 java 方法:
// Gets this from the "org.apache.struts2.util.TokenHelper" class in the struts2-core jar
String token = TokenHelper.getTokenName();
这是“TokenHelper.java”中的方法:
/**
* Gets the token name from the Parameters in the ServletActionContext
*
* @return the token name found in the params, or null if it could not be found
*/
public static String getTokenName() {
Map params = ActionContext.getContext().getParameters();
if (!params.containsKey(TOKEN_NAME_FIELD)) {
LOG.warn("Could not find token name in params.");
return null;
}
String[] tokenNames = (String[]) params.get(TOKEN_NAME_FIELD);
String tokenName;
if ((tokenNames == null) || (tokenNames.length < 1)) {
LOG.warn("Got a null or empty token name.");
return null;
}
tokenName = tokenNames[0];
return tokenName;
}
此方法中的第一行返回 null
:
Map params = ActionContext.getContext().getParameters();
下一个 LOC,“params.containKey(...)”会抛出 NullPointerException,因为“params”为 null。
当正常调用此操作时,运行良好。然而,在JUnit测试过程中,出现了这个空指针。
我的测试类如下所示:
@Anonymous
public class MNManageLocationActionTest extends StrutsJUnit4TestCase {
private static MNManageLocationAction action;
@BeforeClass
public static void init() {
action = new MNManageLocationAction();
}
@Test
public void testGetActionMapping() {
ActionMapping mapping = getActionMapping("/companylocation/FetchCountyListByZip.action");
assertNotNull(mapping);
}
@Test
public void testLoadStateList() throws JSONException {
request.setParameter("Ryan", "Ryan");
String result = action.loadStateList();
assertEquals("Verify that the loadStateList() function completes without Exceptions.",
result, "success");
}
}
在我切换到使用 StrutsJUnit4TestCase 后,ActionContext.getContext() 至少不再为 null。
知道为什么 .getParameters() 返回 null 吗?
最佳答案
您需要在测试方法中自行初始化参数映射。此外,如果您想获取 token 名称,则需要将其放入参数映射中。
Map<String, Object> params = new HashMap<String, Object>();
params.put(TokenHelper.TOKEN_NAME_FIELD,
new String[] { TokenHelper.DEFAULT_TOKEN_NAME });
ActionContext.getContext().setParameters(params);
关于maven - ActionContext.getContext().getParameters() 在 StrutsJUnit4TestCase 期间返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21291106/
我正在使用 浏览要上传的图像文件。但是当我使用时 String imageUrl = request.getParameter("file"); out.println("logofile" +
这个问题已经有答案了: Missing Elements in HTTP Request - Null or Empty? (2 个回答) 已关闭 9 年前。 我有一个 html 形式的文本字段。
请在此提供帮助,getParameter 仅打印标签中 String 元素的第一部分。 这是选择标签 > 这是 servlet 中的代码 PrintWriter pw = response.
我有一个 JSP 页面,它加载另一个 jsp 页面的内容,它工作得很好。问题是我想通过 request.getParameter("cfgname")到这个内容页面,这样当它被加载到主 JSP 中时,
我在 JSP 中有以下页面: 如果我启动,它会在领事上显示“null”,为什么?抱歉问这个非常基本的问题! 最佳答案 您需要将此代码分布在两个不同的 JSP 中
所以,我在从客户端 jsp 检索信息时遇到问题。 javascript 执行,并打印警报,但查询在 java servlet 中变为 null,然后 null 被写入记录器。我似乎无法弄清楚为什么查询
我当前有一些输入按钮的实例,并在单击按钮时使用 request.getParameter 来请求。像这样: String t = request.getParameter("test");
我使用 beans/form 处理在登录屏幕上获取输入参数,然后使用这些参数尝试将用户登录到应用程序中。 但是我收到错误 - org.apache.jasper.JasperException: /l
我正在尝试调用getParameter。一切正常,但是当我单击编辑链接时,它给出了 NullPointerException 。为什么取不到值? JSP CODE:
我想为属性“host”提供所请求参数“ip”的值,使用form.html中的Get方法提交 这是我的index.jsp Ip Not
Login Controller: package test; import java.io.IOException; import java.util.ArrayList; import javax
我想要单选按钮的参数值。 Official Only All 如何在 java 中检查是否选择了哪一个? 最佳答案 使用HttpServletRequest#getParameter()并将输入字段
我正在尝试使用 getParameter 从 JSP 获取值,其中包括 ü、é、à 等。但是在 servlet 中获取错误的值。我用 firebug 检查了内容类型,发现 Content-Type
这个问题已经有答案了: How do I compare strings in Java? (23 个回答) 已关闭 9 年前。 我在 Java 中遇到 request.getParameter("p
IN JAVA CODE IN JSP as below i m getting null value for field"noOfRecords". if (request.getMet
我在 search.java 中有一个使用 getParameter 和 xmltransform 的代码。我正在使用 search.java 根据给定的标题搜索书籍数据库。对于所有标准,我得到输出。
我真的很难理解为什么我被告知 getParameter 返回一个对象,我需要在下面的代码中将其转换为字符串?在 String timeTaken 时,我收到错误“类型不匹配:无法从 void 转换为
这是我的第一个 servlet,它将内容传递给下一个 servlet.. response.setContentType("text/html"); PrintWriter out=r
如何将 html 形式的按钮与 servlet 绑定(bind)在一起? 例如,我尝试这样做: Submit 在 servlet 中: if(request.getParameter("submitB
我发现这个问题非常令人困惑(但也很有趣),因此我想向这里的人们寻求见解。 我一直在自学 JSP 和相关技术。我要做的是将参数从 JSP 检索到 servlet,然后将它们与 If() 一起使用。 这是
我是一名优秀的程序员,十分优秀!