[英]NullPointerException when using an interceptor in Struts 2
[英]NullPointerException in custom Interceptor
我是新的struts framework。在這里我試圖創建自定義攔截器,我只是將給定的用戶名轉換為大寫並在jsp中顯示它。
我的index.jsp代碼是:
<s:form action="login"> <s:textfield name="name" label="UserName"></s:textfield> <s:submit value="Submit"></s:submit> </s:form>
攔截器代碼為:
public class CustomInterceptorExample implements Interceptor
{
@Override
public void destroy()
{
// TODO Auto-generated method stub
}
@Override
public void init()
{
// TODO Auto-generated method stub
System.out.println("Custom Interceptor called.....");
}
@Override
public String intercept(ActionInvocation ai) throws Exception
{
// TODO Auto-generated method stub
ValueStack vs=ai.getStack();
String name=vs.findString("name"); //here i am getting exception
System.out.println(name);
vs.set("name", name.toUpperCase());
System.out.println("intercept() called..."+ai.invoke());
return ai.invoke();
}
}
Struts.xml代碼是:
<struts>
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="logerName" class="com.strutsLearner.CustomInterceptorExample"></interceptor>
</interceptors>
<action name="product" class="com.strutsLearner.Product">
<result name="success">welcome.jsp</result>
</action>
<action name="login" class="com.strutsLearner.Login">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="logerName"></interceptor-ref>
<result>LogResult.jsp</result>
</action>
</package>
</struts>
堆棧跟蹤為:
java.lang.NullPointerException
com.strutsLearner.CustomInterceptorExample.intercept(CustomInterceptorExample.java:31)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
誰能告訴我我在哪里弄錯了。
您需要在stuts.xml文件中編寫defaultStack攔截器
<interceptor-ref name="defaultStack"></interceptor-ref>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.