Skip to content
Advertisement

How to pass an action name using hyperlink in Struts 2?

I have a hyperlink named "Click for new User". Once I click the link, I got NullPointerException instead of opening RegisterPage.jsp page.

I post my code here, I can’t find my mistake

index.jsp:

JavaScript

struts.xml:

JavaScript

Login.java(Action Class):

JavaScript

Exception:

JavaScript

Advertisement

Answer

When you click on hyperlink you don’t supply the request URL with parameters username and password (the parameters aren’t set by the params interceptor), so the values of this properties are null but the validate() method is called before the action is executed because your action extend ActionSupport which implemented Validateable and ValidationAware that allows the validation interceptor (which is a part of the defaultStack) execute validate() method where you could perform manual checks and provide ActionErrors, FieldErrors, and ActionMessages. But there’s no reason to do so if you don’t provide the values for the properties. You should either submit the form or set the parameters in the url like

JavaScript

But the problem is the parameter values (username and password) are sensitive information and shouldn’t pass in the URL. And you should use either Ajax post or submit the form with the POST request like in this example.

When using manual validations very useful to utilize the Apache commons validator

JavaScript

The similar code could be done for ActionErrors and/or ActionMessages. Other validators are available in the commons-validator package, or you could use Struts2 validators that is out of this question.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement