Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
525 views
in Technique[技术] by (71.8m points)

java - Interceptor can't access Action Parameters

I'm creating an example for struts2 interceptors. I created a simple login page and used a custom interceptor class to encrypt the input. But the interceptor is reading the values of input from ValueStack as null.

I don't understand what am I doing wrong. I suppose struts.xml and interceptor class are enough data for this. If you need some more of my code, please tell.

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.devMode" value="true" />
    <package name="myPackage" extends="struts-default">
    <interceptors>
            <interceptor name="encrypt" class="com.keyur.struts2.interceptors.EncryptDecryptInterceptor"/>
        </interceptors>
        <action name="validatorAction" class="com.keyur.struts2.ActionClasses.validatorClass" method="execute">
        <interceptor-ref name="encrypt"></interceptor-ref>
        <result name="success">/success.jsp</result>
        <result name="input">/index.jsp</result>
        </action>
    </package>
</struts>

Interceptor .java File

package com.keyur.struts2.interceptors;

import com.keyur.struts2.ActionClasses.validatorClass;
import com.keyur.struts2.beans.EncryptorDecryptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.util.ValueStack;

public class EncryptDecryptInterceptor implements Interceptor {

        EncryptorDecryptor encdec = new EncryptorDecryptor();

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    @Override
    public void init() {
        // TODO Auto-generated method stub

    }

    @Override
    public String intercept(ActionInvocation arg0) throws Exception {
        // TODO Auto-generated method stub

        String result = arg0.invoke();

        ValueStack stack = arg0.getStack();
        String username = stack.findString("username");
        String password = stack.findString("password");

        System.out.println("Username: "+((validatorClass)stack.peek()).getUsername());
        System.out.println("Password: "+((validatorClass)stack.peek()).getPassword());
        //System.out.println(username);
        //System.out.println(password);

        //stack.set("username", encdec.encryptText(username));
        //stack.set("password", encdec.encryptText(password));

        return result;
    }
}

EncryptorDecryptor is the a separate class which I have defined and it is working correctly on it's own.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want to access parameters, the params interceptor should go first

<action name="validatorAction" class="com.keyur.struts2.ActionClasses.validatorClass" method="execute">
   <interceptor-ref name="params"></interceptor-ref>
   <interceptor-ref name="encrypt"></interceptor-ref>
   <interceptor-ref name="defaultStack"></interceptor-ref>
   <result name="success">/success.jsp</result>
   <result name="input">/index.jsp</result>
</action>

Your problem is that those parameters don't go to the valueStack, and you should probably get them from the action context.

Map params = ActionContext.getContext().getParameters();

But after params interceptor they should be there.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...