Hello Avijit,

It looks like you are getting confused from the concept of Events and
Services in java. It is very well explained in the OFBiz tutorial:
https://cwiki.apache.org/confluence/display/OFBIZ/OFBiz+Tutorial+-+A+Beginners+Development+Guide+for+18.12

Now talking about your code snippet:

You are calling an event from a controller so you need to get all the
parameters from the method request.getParameter("html_form_elements_name").


And once you have read all the values in events then from here you should
pass these values to the service where you can take care of business logic
and CRUD operations. Basically events are used to do the validations and
extra manipulations with the form parameters.

If you don't want to go with the Events route then you should change your
controller.xml entry as shown below:

<request-map uri="createStudent">
<security https="true" auth="true"/>
<event type="service" invoke="createStudent"/>
<response name="success" type="view" value="newStudent"/>
<response name="error" type="view" value="errorPage"/>
</request-map>

Here you will notice that I have used the event type as "service" where you
don't need to specify the path of the service. You can specify the service
definition and implement your service either in Java/Groovy/XML(Minilang).
Services in OFBiz are session-less, so if you wish to do handling of
HttpSession object then you need to do it in the Events.

All these things are very well explained in the tutorial link which I have
shared above. There is also a very good reference available for the
comparison between Events and Services. Please take a look at it.

Thank you.

--
Kind Regards,
Ashish Vijaywargiya
Vice President of Operations
*HotWax Systems*
*Enterprise open source experts*
http://www.hotwaxsystems.com



On Sat, Jul 9, 2022 at 7:32 AM Avijit Bose <bose.avi...@gmail.com> wrote:

> Hi Nicolas,
>
> First... I have a ftl file like this. Here I am calling "createStudent" in
> the controller through "<@ofbizUrl>createStudent</@ofbizUrl>"
> *************************
> <form id="regForm" method="post"
> action="<@ofbizUrl>createStudent</@ofbizUrl>" name="regForm"
> enctype="multipart/form-data">
> <body>
> ..... blah blah blah
> </body>
> </form>
> *************************
>
> Second.... my controller as shown below....
> *************************
> <request-map uri="createStudent">
> <security https="true" auth="true"/>
> <event type="java" path="com.sms.events.StudentServices"
> invoke="createStudent"/>
> <response name="success" type="view" value="newStudent"/>
> <response name="error" type="view" value="errorPage"/>
> </request-map>
> *************************
> Third.... from the controller above I am going to a java file called
> "StudentServices" and invoking a method in the java file called
> "createStudent".
>
>
> Fourth... In the "createStudent" method of the java file... I have the
> following...
> **************************
> public static String createStudent(HttpServletRequest request,
> HttpServletResponse response) {
> Delegator delegator = (Delegator) request.getAttribute("delegator");
> LocalDispatcher dispatcher = (LocalDispatcher)
> request.getAttribute("dispatcher");
> GenericValue userLogin = (GenericValue)
> request.getSession().getAttribute("userLogin");
> Map<String, Object> result = ServiceUtil.returnSuccess();
>
> try {
>         GenericValue smsSubjectMaster =
> delegator.makeValue("smsSubjectMaster");
>         // Auto generating next sequence of studentId primary key
>         smsSubjectMaster.setNextSeqId();
>         // Setting up all non primary key field values from context map
>         smsSubjectMaster.setNonPKFields(context);
>         // Creating record in database for smsStudentMaster entity for
> prepared value
>         smsSubjectMaster = delegator.create(smsSubjectMaster);
>         //subjectId = smsSubjectMaster.getString("studentId");
>         //context.putIfAbsent("subjectId", subjectId);
>           context.put("subjectId", subjectId);
>         result.put("subjectId", smsSubjectMaster.getString("subjectId"));
>         Debug.log("==========smsSubjectMaster record created successfully
> with subjectId: "+smsSubjectMaster.getString("subjectId"));
>
>     } catch (GenericEntityException e) {
>         Debug.logError(e, module);
>         return ServiceUtil.returnError("Error in creating record in
> smsSubjectMaster entity ........" +module);
>     }
>
> }
> **************************
> In the try/catch block... pls notice this...
> "smsSubjectMaster.setNonPKFields(context);". This 'context' I am talking
> about. I want the context in this method ...
>
> public static String createStudent(HttpServletRequest request,
> HttpServletResponse response) {
> // I NEED CONTEXT HERE
> }
>
> AND NOT IN THIS METHOD
>
> public static Map<String, Object> createStudent(DispatchContext dctx,
> Map<String, Object> context){
>  ?? HERE I AM ALREADY PASSING CONTEXT AND GETTING IT
> }
>
> Now...
>
> I see a 'globalContext' in this link...
>
> https://cwiki.apache.org/confluence/display/OFBIZ/Variables+always+available+in+screen+context
>
> But I think globalcontext is not the same as context. From the link above I
> could pass the variables mentioned in the link from ftl to java by setting
> screen parameters, but no parameter named 'context' in the link above.
>
> Question: How do I get context in a method where I am passing
> "HttpServletRequest request, HttpServletResponse response" as shown above.
>
> regards
> Avijit
>
>
> On Fri, Jul 8, 2022 at 1:05 PM Nicolas Malin <nicolas.ma...@nereide.fr>
> wrote:
>
> > Hello Avijit,
> >
> > Can you share some peace of code, I didn't understand your ftl to java ?
> >
> > You have a ftl template and go to java service through request call or
> > you want to call a java code on a ftl code ?
> >
> > Nicolas
> >
> > On 28/06/2022 12:45, Avijit Bose wrote:
> > > Dear Sir,
> > >
> > > Can the 'context' be passed from a ftl file to java?
> > >
> > > I see a 'globalContext' in this link...
> > >
> >
> https://cwiki.apache.org/confluence/display/OFBIZ/Variables+always+available+in+screen+context
> > >
> > > My goal is to use context here..
> > > -----------------------
> > > dbName.setNonPKFields(context);
> > > -------------------------
> > > in a method...
> > > ------------------------
> > > public static String createSubscriber(HttpServletRequest request,
> > > HttpServletResponse response) {  }
> > > -----------------------
> > > can I use 'globalContext' in place of 'context'?
> > >
> > > regards
> > > Avijit
> > >
> >
> >
>

Reply via email to