CQ5.5: Accessing a referring resource in a osgi servlet - osgi

I'm curious if there is a way to access a referrer resource from inside an OSGI Servlet.
Example:
I have a form component
Form component submits to /bin/form (a servlet)
Inside the servlet at /bin/form I would like to access the form component Resource - not sure how to do this part
any ideas/thoughts would be greatly appreciated.
thank you,
Brodie

You may add a hidden value with the component resource path:
<input type="hidden" name="resourcePath" value="${resource.path}"/>
value will be available as resourcePath parameter in the servlet code:
String resourcePath = request.getParameter("resourcePath");
You should check if the resource path is a valid value, as a malicious user can set anything they want there.
Also, please consider the second approach: instead of binding the servlet to a static path /bin/form you may bind it to your component resourceType. Let's assume that this resource type is myapp/component/form. In this case you may declare servlet as follows:
#SlingServlet(resourceTypes="myapp/component/form", methods="POST", selectors="form")
public FormServlet extends SlingAllMethodsServlet {
public doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) {
Resource formResource = request.getResource();
// ...
}
}
Form code should look like this:
<form action="${resource.path}.form.html" method="post">
<!-- your form contents here -->
</form>
Advantages of this approach:
you can get the form resource as simply as request.getResource(),
you don't need to perform additional resource path parameter validation,
it is more RESTful.

Related

I want to assign Object field eg. greeting.method (which can be post or get ) to Form Method attribute using thymeleaf spring boot

Generally the code is -->
Image of code, click here to see the code!
I want that method which is hardcoded as "post" need to come from greeting object eg. (greeting.method)
How can I achieve that any suggestions?
So just to make it clear, you want to read the url action and method from a variable, instead of hardcoding them in thymeleaf
In that case that is actually very simple:
Pass url and method variables to the model by defining the following in your controller
#ModelAttribute("url") public String url() { return "foo/bar"; }
#ModelAttribute("method") public String method() { return "POST"; }
Define the url and method with thymeleaf: <form th:action="${url}" th:method="${method}" ...>

How to get the url from the application inside a thymeleaf processor?

In my current spring project, I created a custom thymeleaf processor. The implementation for this processor needs to retrieve the url for the application, liek the value returned for ${pageContext.request.contextPath} in jsp pages. Someone knows how to get that?
Inside view:
In Thymeleaf, the equivalent of JSP's ${pageContext.request.contextPath} would be #{ }, ie. if you have a url in jsp like ${pageContext.request.contextPath}/login then in thymeleaf it would be #{/login}. The /[[ - ]]/ syntax is used by Thymeleaf to evaluate variables in javascript, like var url = /*[[#{/login}]]*/;
Ref: http://www.thymeleaf.org/doc/articles/standardurlsyntax.html
Inside Processor Class:
First you need to cast context to IWebContext and then get HttpServletRequest object calling getRequest and finally you will get context path by calling request.getContextPath(). You may want to check if context is a type of IWebContext using instanceof operator, I have skipped that here.
final HttpServletRequest request = ((IWebContext) context).getRequest();
System.out.println("contextpath: " + request.getContextPath());

Liferay 6.1 + Spring MVC Portlet 4.0: Friendly URL Mapping

I'm trying to get friendly URL mapping to work for a Spring MVC portlet inside Liferay 6.1 and fail.
My additions to the liferay-portlet-xml are according to the manual and blog examples available and as follows:
<friendly-url-mapper-class>com.liferay.portal.kernel.portlet.DefaultFriendlyURLMapper</friendly-url-mapper-class>
<friendly-url-mapping>search</friendly-url-mapping>
<friendly-url-routes>com/.../friendly-url-routes.xml</friendly-url-routes>
with the friendly-url-routes.xml being
<!DOCTYPE routes PUBLIC "-//Liferay//DTD Friendly URL Routes 6.1.0//EN"
"http://www.liferay.com/dtd/liferay-friendly-url-routes_6_1_0.dtd">
<routes>
<route>
<pattern>/{match}</pattern>
<generated-parameter name="foo">{match}</generated-parameter>
</route>
</routes>
My Spring MVC controller goes like
#Controller
#RequestMapping("VIEW")
public class CarModelController {
#ActionMapping
public void action(#RequestParam("foo") final String testParam,
final ActionRequest request, final ActionResponse response) {
this.logger.info("default action");
this.logger.info("testParam = {}", testParam);
}
#RenderMapping
public String render(final RenderRequest request, final RenderResponse response) {
this.logger.info("default render");
return "index";
}
}
If I call my portlet using /baseurl/-/search/bar only the render phase output occurs, the action method isn't called.
If I create a link to this page using
<portlet:actionURL var="lastStepUrl">
<portlet:param name="foo" value="bar" />
</portlet:actionURL>
the URL Liferay generates looks like /baseurl/-/search/bar?p_auth=sometoken&p_p_lifecycle=1. It executes the action phase correctly and I'm also able to call that URL directly. It does, however, include the p_auth and p_p_lifecycle parameters that I want to get rid of.
Any suggestions are welcomed warmly.
I don't know much about Friendly URL in Liferay.
But I believe you can't completely remove p_p_lifecycle from url because.
this parameter tells Liferay which action to perform. This paramater has two values (0 or 1).
0 tells Liferay to just render the portlet,
whereas 1 tells Liferay to call the process Action Method.
Lets say you want to remove from certain action URL then can do it like this
<pattern>"your URL pattern"</pattern>
<implicit-parameter name="p_p_lifecycle">1</implicit-parameter>
<implicit-parameter name="javax.portlet.action">"Your action"</implicit-parameter>
As we know 1 for action phase we can hard code and put it into routes.xml file.same way for any render URL we can put 0
To remove p_auth try putting below properties in portal-ext.properties file
portlet.add.default.resource.check.enabled=false
auth.token.check.enabled=false

Neither BindingResult nor plain target object for bean in spring 3

Hi I am facing an issue and looked all over internet but still not able to find out the root cause. I am posting my code snippet please help me out as I am new to spring 3. I am using modelAttribute in form and what I want that in controller all the values from request should be backed in the object so that I can perform validation and other business logic.
I know there is mistake only in my controller.
1) index.jsp
<form:form action="login" method="POST" modelAttribute="login">
<table>
<tr><td>User Id:</td><td><form:input path="userId"/></td></tr>
<tr><td>Password:</td><td><form:password path="userPassword"/></td></tr>
<tr><td></td><td><input type="submit" value="Login"/></td></tr>
</table>
</form:form>
2) Controller
#RequestMapping(value="/login/", method=RequestMethod.POST)
public String login(#ModelAttribute("login") #Valid Login login,BindingResult result)
{
System.out.println("We have entered into controller class");
if(result.hasErrors())
{
System.out.println("Errors:"+result.getFieldError("userReject"));
return "redirect:/login";
}
else
{
return "home";}
}
}
3) JBoss Log
04:35:29,067 ERROR [org.springframework.web.servlet.tags.form.InputTag] (http--0.0.0.0-8090-1) Neither BindingResult nor plain target object for bean name 'login' available as request attribute: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'login' available as request attribute
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141) [spring-webmvc-3.0.5.Release.jar:3.0.5.RELEASE]
The problem is not in the method you posted, which handles the login form submission. It's in the method used to display the form. The form needs to populate its fields from a bean named "login", and you didn't place any bean named "login" in the model, i.e. in a request attribute.
Side note: a login form should never use GET. It should use POST. You really don't want the password to appear in the browser address bar. And you don't want it to appear in the browser history, the server and proxy logs, etc.

Trying to pass view implicitly using mvc:view controller

I have a Spring configuration file with MANY entries such as the one below.
<mvc:view-controller path="/test/one" view-name="one.xml" />
<mvc:view-controller path="/test/two" view-name="two.xml" />
<mvc:view-controller path="/test/three" view-name="three.xml" />
Now I am integrating controller code and I am trying to avoid having to have a request mapping for every single page. Is there a way for Spring MVC to implicitly find the page with the view as specified in the controllers.xml.
So instead of returning a string as the View. I would like a method where I was returning void or just the model to the page and for it to find the page
#RequestMapping(value = "/test/one", method = RequestMethod.GET)
public String getOne(HttpServletRequest request, Model model) {
// Business Logic
return "one.xml" // would like to be able to return void here or just the mode
}
The idea of mvc:view-controller is precisely to avoid having to implement a controller whan its only job would be to dispatch to a view. Just having this tag in the XML is sufficient. You don't need a controller.
See http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-view-controller
If you have controller logic and thus need a controller, then the mvc:view-controller element should be removed, and be replaced by the actual controller, which dispatches to the appropriate view. You may avoid specifying a view name explicitely here using a RequestToViewNameTranslator, as explained here.

Resources