This ain't going to well, I am sorry for taking your time! However, I
must be close to the solution since I found an example on Internet
which I managed to get to work, more or less like I want it to work.

But when going back to my own code, it still doesn't work, even though
I tried to do exactly as the example I found and made work. It looks
like the validation is never performed and, hence, no message returned
to the client about what information is missing/incorrect. I do write
something in the execute() of the RegisterQAFormAction class and it is
written to the console even though I don't have any text in required
inputfields.

Regarding the default interceptor stack; I think I do use it, because
as far as I understand I haven't defined anything else.

I found one comment that "<s:head/>" is needed in the jsp, is it
required or just for formatting?

I include my updated code here:

RegisterQAFormAction  class:

package org.nicsoft.application.mobiletraining.qa;

import com.opensymphony.xwork2.ActionSupport;

public class RegisterQAFormAction extends ActionSupport {

        private String heading;
        private String answer;

        
        public String execute() throws Exception {
                
                System.out.println("INSIDE EXECUTION***************** ");
              return SUCCESS;
    }   

    public void setHeading(String heading){
        this.heading = heading;
    }

    public String getHeading(){
        return this.heading;
    }

    public void setAnswer(String answer){
        this.answer = answer;
    }

    public String getAnswer(){
        return this.answer;
    }
}

ReqisterQAFormAction-validation.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
      "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
      "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd";>

<validators>

  <field name="heading">
    <field-validator type="requiredstring">
      <param name="trim">true</param>
      <message>Heading is required</message>
    </field-validator>
  </field>

   <field name="answer">
    <field-validator type="requiredstring">
      <param name="trim">true</param>
      <message>Answer is required</message>
    </field-validator>
  </field>

</validators>

part of struts.xml:

<package name="org.nicsoft.application.mobiletraining.qa"
extends="struts-default">
        <action name="postRegisterQAForm"
class="org.nicsoft.application.mobiletraining.qa.RegisterQAFormAction">
                        <result name="input">/jsp/forms/registerqa.jsp</result> 
                
                <result>/jsp/forms/registerqa.jsp</result>
        </action>
        <action name="getRegisterQA">
            <result>/jsp/forms/registerqa.jsp</result>
        </action>       
</package>


registerqa.jsp:


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd";>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../resources/css/mobiletrainingstyle.css"
type="text/css" media="screen">
<title>Insert title here</title>
<s:head/>
</head>

<body>

<h3 class="redtext"><s:property value="message" /></h3>

        <s:form action="postRegisterQAForm.action">                     
                <s:textfield name="heading" label="Fråga" required="true"/>
                <s:textfield name="answer" label="Svar" required="true"/>       
        
                <s:submit value="Spara" align="center"/>
        </s:form>
</body>
</html>

Thanks again!

Regards,
Niklas

2009/5/25 Dave Newton <newton.d...@yahoo.com>:
> Niklas Johansson wrote:
>>
>> I want to use Requieredstring validator. The problem is that my
>> application is not complaining if I do leave the input field empty.
>
> Are you using the default interceptor stack?
>
>> Can anyone tell me what is the problem?
>
> I don't see anything immediately obvious, but I'm sleepy.
>
>> Q: "why you're using ParameterAware" ... "it's generally cleaner to
>> just use the action properties"
>>
>> A: The reason is because I found the instructions on the struts
>> homepage. Using the action properties, I assume I need to define those
>> in the struts.xml, correct?
>
> Define what in the struts.xml? The "struts homepage" doesn't have
> instructions for doing *anything*, so you'll have to be more specific as to
> what you were reading so we can correct it.
>
> The probable answer to your question is "no". One of the main features (IMO)
> of S2 is that it eliminates a lot of back-and-forth between HTML forms and
> actions, meaning your code could be written follows, getters and setters
> elided. (And why make the action properties public if you have
> getters/setters for them?)
>
> public class RegisterQAFormAction extends ActionSupport {
>
>    private String message;
>    private String heading;
>
>    public String execute() throws Exception {
>        System.out.println(heading);
>        System.out.println(answer);
>
>        // Don't know what this is trying to do, so ignoring.
>        //QAHandler handleQA = new QAHandler();
>        //handleQA.storeQA(param);
>
>        setMessage("Ärende " + heading + " registrerat");
>        if (getHeading() == null) {
>            return ERROR;
>        }
>
>        return SUCCESS;
>    }
>
> }
>
> Note that heading won't be null if the user doesn't enter a value--it will
> be an empty string, so in all probability this is a bug in your code.
>
>> Q: "That class is part of the showcase app, not part of Struts itself."
>>
>> A: So what is the solution? The instructions wasn't that clear about
>> where the class came from on the instruction page:
>> http://struts.apache.org/2.1.6/docs/using-field-validators.html
>
> Solution to what problem? You don't need that class to use the validation as
> you've written it above.
>
> Dave
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
> For additional commands, e-mail: user-h...@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to