Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simple struts2 validation needed on my action class

 
Rob Hope
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try the following example Struts2 Validation.
 
Rob Hope
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9280
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Hope
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9280
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic