wood burning stoves 2.0*
The moose likes Struts and the fly likes simple struts2 validation needed on my action class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "simple struts2 validation needed on my action class" Watch "simple struts2 validation needed on my action class" New topic
Author

simple struts2 validation needed on my action class

Rob Hope
Ranch Hand

Joined: Mar 31, 2012
Posts: 42
I have created a simple CRUD java program using Hibernate
However I need to implement form validation one of the content entry boxes to prevent null values.
Can anyone suggest the simplest way this could be achieved?
I have posted my action.class below

Mohana Rao Sv
Ranch Hand

Joined: Aug 01, 2007
Posts: 485

Try the following example Struts2 Validation.


ocjp 6 — Feeding a person with food is a great thing in this world. Feeding the same person by transferring the knowledge is far more better thing. The reason is the amount of satisfaction which we get through food is of only one minute or two. But the satisfaction which we can get through the knowledge is of life long.
Rob Hope
Ranch Hand

Joined: Mar 31, 2012
Posts: 42
ive tried this method however because my action class is using data entered from my customer model i get the following error which im not sure there is any solution

Cannot make a static reference to the non-static method getName() from the type Customer
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

Rob Hope wrote:Cannot make a static reference to the non-static method getName() from the type Customer


You cannot invoke a non-static method on a class. For instance:



This is a very basic Java concept. You should spend some time with the Java Tutorial. The better you know Java, the easier time you will have with technologies that are based on it.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Rob Hope
Ranch Hand

Joined: Mar 31, 2012
Posts: 42
Thanks Joe

I managed to the the vaildation method accepted



but it causes me a struts2 NPE
Struts has detected an unhandled exception:

I'm not sure why because both input and success are mapped

<result name="success">pages/customer.jsp</result>
<result name="input">pages/customer.jsp</result>
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

Rob Hope wrote:
Struts has detected an unhandled exception:


That usually means that you have an exception occurring in your code and you are not catching it. Your best bet is to wrap all the code that is getting invoked by Struts with try-catch blocks.
Rees Byars
Greenhorn

Joined: Feb 13, 2012
Posts: 12
If the parameter is on the form that the user is submitting, then if they leave it blank the value should be an empty string, "", not null.

But anyway, just wanted to recommend that, if you are going to validate the parameter manually (instead of using the validation framwork), then I suggest using the Commons StringUtils. This class should be on your classpath already in one of the Commons libraries needed for Struts2.

So, instead of hecking the length of the string, checking for null, etc., you can just do:

Rob Hope
Ranch Hand

Joined: Mar 31, 2012
Posts: 42
Hi Rees

Thanks for your help but I changed my vaildation method to use struts2 .xml form validation instead
It works! but i'm still unable to get the UI error message to showup on my .jsp page - can you help?

http://www.coderanch.com/t/579104/Struts/xml-form-validation-UI-error

Rees Byars
Greenhorn

Joined: Feb 13, 2012
Posts: 12
Sure. You can include them in a couple of ways. Generally, I do the following to place the errors next to the text fields:




Also, it can be good to include a message at the top of the form letting the user no that the form has errors:



The hasFieldErrors() method is available if your action extends ActionSupport.

Also, if you don't want to place the error messages next to the test fields, you can display them all together at the top of the page using this:



The fielderror tag displays them all if the param tag does not specify a field.

Hope it helps!
Rob Hope
Ranch Hand

Joined: Mar 31, 2012
Posts: 42
nope.. not sure what im doing wrong validates just fine but can't get any UI message to show


humm still not seeing any warning message




What does setting a 'Key' variable for the field actual do?
Rees Byars
Greenhorn

Joined: Feb 13, 2012
Posts: 12
The "key" attribute set the name, label, and value for the element. It is particularly useful for basing the language of test on the user's i18n locale. See this page of the Struts2 Beginner's Tutorial: http://struts.apache.org/2.x/docs/message-resource-files.html.

And definitely go through the tutorials if you have not. It is a really well-written introduction to the basics of using Struts2.

These are great resources:

Beginner's Guide: http://struts.apache.org/2.x/docs/getting-started.html
Core Developer's Guide: http://struts.apache.org/2.x/docs/guides.html
Rob Hope
Ranch Hand

Joined: Mar 31, 2012
Posts: 42
Hi Rees

Your right those are good links & i tried adding the key variables and comprehending properties file but still not seeing any UI messages.
I think we are going down the wrong avenue here because the xml validation is working; so the correct fields are being identified its only the messages that are not being shown.

It makes me think the problem is on the .jsp view not holding the required error field

<s:form action="addCustomerAction">
<s:fielderror><s:param>name</s:param></s:fielderror>
<s:textfield name="name" key="global.name" label="OS" value=""/>
<s:textarea name="notes" key="global.notes" label="Notes" value="" cols="50" rows="5" />
<s:submit validate="true" />
</s:form>
tyte kyat
Ranch Hand

Joined: May 17, 2011
Posts: 40
Rob Hope wrote:Thanks Joe

I managed to the the vaildation method accepted



but it causes me a struts2 NPE
Struts has detected an unhandled exception:

I'm not sure why because both input and success are mapped

<result name="success">pages/customer.jsp</result>
<result name="input">pages/customer.jsp</result>


try this mate. just pay attention after if statement and getUserName() method

Rob Hope
Ranch Hand

Joined: Mar 31, 2012
Posts: 42
I gave up on that method all together when I moved to .xml form vaildation instead
I understand it doesnt require any if statements just action-vaildation.xml file put in the right place.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: simple struts2 validation needed on my action class
 
Similar Threads
Null Point Exception
java.lang.NullPointerException com.mkyong.customer.action.CustomerAction.listCustomer
StrutsPrepareandExecutefilter error
Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
execute method of ActionSupport Class