Syntax for the customized methods when extending Liferay MVCPortet - liferay

I am using Liferay 6 for development.
I have one query with respect to extending MVCPortlet class and providing the methods.
In some of the examples, during extending MVCPortlet and providing customized methods, is it mandatory to provide the parameters as ActionRequest and ActionResponse to the method as shown below:
public void addBook(ActionRequest request, ActionResponse response) {
}
So my question is, is this syntax mandatory?
Thanks in advance.

Yes, it's mandatory as this implements an action handler. You can also override GenericPortlet's processAction with the same parameters or use the #ProcessAction annotation, but in all cases you'll have to have a way to pass the actual request into the portlet. And that's what the parameters are for.
If you don't provide the parameters, the method will not be found by MVCPortlet's reflection-based resolving of the actual action name.

Related

ServiceStack Swagger DTO won't Exclude

I'm having a problem with excluding a specific DTO from Swagger in my ServiceStack application. Here's my setup:
[Route("/lists", "GET")]
public class GetLists : IReturn<GetListsResponse>
{
}
[Route("/lists", "POST")]
[Exclude(Feature.Metadata)]
public class CreateList : IReturn<CreateListResponse>
{
}
The behavior I'm expecting is the Swagger will remove the POST request docs but not the GET. Instead I'm getting both of them listed. Any help with what I'm doing wrong is appreciated.
UPDATE: I have tried adding the following attribute to no success:
[Restrict(VisibilityTo = RequestAttributes.None)]
The visibility of different Request DTO's should now be resolved from this commit that's available from v4.0.55 that's now available on MyGet.
From looking at the documentation, it looks like you can either exclude properties of a DTO or all services using a DTO -- but it doesn't say anything about excluding only certain verbs.
You can restrict Visibility using the [Restrict] attribute. (see documentation). This is a class based attribute and should be placed on your Service class. Visibility affects whether or not the service shows up on the public /metadata pages (& I am hoping for Swagger as well).
Have you tried the below?
[Route("/lists", "POST")]
[Restrict(VisibilityTo = RequestAttributes.None)]
public class CreateList : IReturn<CreateListResponse>
{
}

GenericDTO and JSF

I am currently reading Adam BienĀ“s book "Java EE Patterns: Rethinking Best Practices".
In chapter "Transfer Object and Data Transfer Object" on page 273 he introduces a generic DTO.
See also: http://www.adam-bien.com/roller/abien/entry/no_duplication_no_decoupling_the
A genericDTO does not follow the bean conventions.
I was wondering how to use generic DTO together with JSF.
JSF if not dynamically used (build components on the flight) makes use of java-beans to bind values to UI-Components (for example #{beanA.beanB.myValue}).
A genric DTO does not provide individual getters for attributes or relations.
Instead i would have to call getters with a string parameter.
So accessing it via #{beanA.beanB.myValue} is not possible in JSF (1.2).
Is it possbile to use the genric DTO pattern together with JSF?
Regards,
Max
I have not read the entirety of Adam's article, so can not comment on when using this pattern is appropriate. However, EL-expressions do support maps with the . or [] operators, i.e. if you have:
#Named
public class MyBean {
private Map<String, Object> attributes;
public Map<String, Object> getAttributes() {
return attributes;
}
}
you can use
#{myBean.attributes.name}
or
#{myBean.attributes['name']}
to get or set the map entry with key "name".

JSF 2.1 - getting FacesContext strategy

I am developing webapp where my MVC controller is JSF 2.1. I have several methods that are based on
FacesContext.getCurrentInstance()
I use this to
put/retrieve values from Flash scope
add messages on view
get request params map
examples:
public void addInfoMessage(String title, String description){
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,title, description));
}
and
public void putFlashMessage(String code, String value){
FacesContext.getCurrentInstance().getExternalContext().getFlash().put(code, value);
}
etc.
I'm just wondering where is proper place to put this methods if I use this on every single managed bean? I consider two options:
a) create class "JSFUtils", where all method are public and static
b) create super class "ManagedBean" with no declared scope and no declared #ManagedBean annotation, but with these public methods. Every managed bean should be child of these class so it will have inherited these methods.
An utility class is the recommended approach. Instead of reinventing your own, you can use an existing JSF utility library, such as OmniFaces which has Faces and Messages utility classes for the purpose.
String foo = Faces.getRequestParameter("foo");
Messages.create(summary).detail(detail).add();
Messages.addGlobalInfo(summary); // Without detail.
Faces.setFlashAttribute(key, value);
You can indeed also abstract it away as a "super bean", but this is not reusable and you would keep repeating yourself in every JSF project. Also, a class can extend from only one class. So if your bean happen to need to extend from another super class, then you're lost.
I would recommend a utility class for the purpose simply because you allow the flexibility to extend other useful classes, such as those that have some common logic that you'd like to share across other beans.
Having said that, a JSFUtils class can grow quite cluttered with time with many many methods and can become very unmanageable. It would be better to categorize the util methods and put them in separate static utility classes.

Alternative to #ModelAttribute

I am using spring mvc for my web application.
I want alternative to #ModelAttribute.
I am setting model object in jsp file by filling form details and right now in controller i am getting it by using #ModelAttribute method parameter as mentioned below.
#RequestMapping(value = "/requestPattern")
public ModelAndView methodName(
#ModelAttribute FormDetail formDetail,BindingResult result,
HttpSession session) {
// I want formDetail object without using #ModelAttribute as a method argument.
}
Please help me out.
At least in Spring 3.X you should be able to omit #ModelAttribute and still have your FormDetail object populated. This is the case with my application. You can see some details here:
Omit ModelAttribute from view

JSF Web application -DAO layer with static methods

Hi I have a question about best practices vs safe programming in building a JSF Web Applicaiton.
I have employee.xhtml page having backing Bean - EmployeeBean. All the variables that I declare in the backing bean are non static and have getter and setter methods. For example:
ArrayList <Employee> alEmployees = new ArrayList<Employee>();
int userId;
The constructor of the BackingBean loads the employees. I acheive this by calling a static method in delegate.
userId = //some value.
alEmployees = EmployeeDelegate.loadEmployees(userId);
The Delegate method calls a static method in DAO Class.
Will the static methods cause any data concurrency issues when n users are using the application at same time? I mean userId 56 seeing userId 75 list when both are using the application same time?
Is it really advisable to have static methods in Delegate and DAO layer?
Please let me know if I was not clear.
Thanks
If the EmployeeDelegate does not hold any class variables which is sensitive to changes caused by method calls and/or has influence on how methods behave, then it's safe to do so.
You however have another major problem with this approach. The delegate should be an interface so that you can easily substitute it with a different implementation. This approach suggests that the delegate is not an interface at all (since it can impossibly have static methods).

Resources