FTL: How to pass variable in header param in ftl - spring-mvc

I have an href tag which is working fine
<a href='<#spring.url "/forgotID.do"/>'>Forgot ID?</a>
//My Controller
#Controller
public class MyControler{
#RequestMapping(value=forgotID,method=RequestMethod.GET)
public ModelAndView forgotIDmethod(#ModelAttribute("model")MyModel model){
}
which is working fine for me
1.Now I wanted to add a param to it I have no idea how to do it Please help me on this
My code is
<a href='<#spring.url "/forgotID.do?loginIdPage='loginURL'"/>'>Forgot ID?</a>
#Controller
public class MyControler{
#RequestMapping(value=forgotID,method=RequestMethod.GET)
public ModelAndView forgotIDmethod(#RequestParam("loginIdPage")
String loginIdPage,#ModelAttribute("model")MyModel model){
System.out.println(" loginIdPage:-"+loginIdPage);
}
I am not getting the value but
I got the URL
http://localhost:8084/Login/forgotID.do?loginIdPage=
2.Again i tried with below scenario
<a href='<#spring.url "/forgotID.do?loginIdPage=/"loginURL/""/>'>Forgot ID?</a>
Getting error in loading the FTL file.
3.When I tried with This
<a href='<#spring.url "/forgotID.do?loginIdPage=/'loginURL/'"/>'>Forgot ID?</a>
Got the URL like this
http://localhost:8084/Login/forgotID.do?loginIdPage=/`
4.When I tried with This
<a href='<#spring.url "/forgotID.do?loginIdPage=\"loginURL\""/>'>Forgot ID?</a>
Request processing failed; nested exception is found in ftl.
Please help how to process it further

Related

unable to send string to same jsp page from controller

i have a jsp page where i have link as follows
dashboars.jsp
<li id="">
<a href="${pageContext.request.contextPath}/student/showAdmissinPage">Admission
</a>
</li>
when clicked on admission link it will go to controller student
#GetMapping(value="/showAdmissinPage")
public String pages()
{
return "studentAdmission";
}
its returning new page with studentAdmission String
i want to receive this studentAdmission String to dashboard.jsp not new page
how can i achieve it with jsp ?
please help me?
Just add String to model. model.addAtributte(studentAdmissionString, "studentAdmission"); ]Then in jsp use ${studentAdmissionString} to print data.
#GetMapping(value="/showAdmissinPage")
public String pages()
{
String s = "studentAdmission";
model.addAtributte("studentAdmissionString", s);
return "dashboard";
}
then in dashboard use ${studentAdmissionString} to print value :)
one of my friend ankit helped with this as follows
modelMap.addAttribute("demo","studentadmission");
and
return "dashboard";
don't return studentadmission .

Spring MVC Model Attribute null

I want do add an ID to a URL in my template:
#Controller
public class DashboardController {
#RequestMapping("/dashboard")
public String index(Model model){
model.addAttribute("provider_id", "1");
return "dashboard";
}
}
In my dashboard.html I want do display the ID.
<a th:href="#{'/cloudservice/' + ${provider_id}}">Show</a>
But the Generated URL is /cloudservice/null. Why isn't the 1 displayed that i put into the model?
I tested this out and I couldn't recreate this issue, this is working perfectly fine for me. Please share the complete controller code and your dependencies here.
One wild guess, your Model class is of type org.springframework.ui.Model, right?
you can use like this http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#link-urls
<!-- Will produce '/gtvg/order/3/details' (plus rewriting) -->
view

How to configure custom servlet in magnolia CMS

I am trying to do registration example in magnolia. I have a registration form, on submission of form control should be transferred to my own written servlet.
snippet for form :
<body>
<form action="./register" method="post">
Name:<input type="text" name="name"><br />
Email Id:<input type="text" name="email"><br/>
<input type="submit" value="Register">
</form>
</body>
Registration servlet class:
public class Registration extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
#Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("This is registration servlet");
}
}
I have configured module descriptor :
<servlets>
<servlet>
<name>RegistrationServlet</name>
<class>com.rbt.registration.Registration</class>
<comment>registration servlet</comment>
<mappings>
<mapping>/register</mapping>
</mappings>
</servlet>
</servlets>
and I have also configured template definition.
But when I click on submit button. It shows error that resource not found.
Please help me.
In your template, when writing html for the form you can also use action="${ctx.contextPath}/register" to have form response directed to uri on which servlet is listening.
Also please go to config:/server/filters/servlets/ and verify that RegistrationServlet is correctly add there.
Same problem (although for different servlet) was also discussed at Magnolia Forum
HTH, Jan
The mapping you configured in the module descriptor will be relative to your context path. E.g. if your base url is http://example.com:8080/public ('public' being your context path), then your servlet will responding to http://example.com:8080/public/register.
Check if that url matches the url your form is submitting to. The action="./register" is a relative path and will depend on the path of the page that contains the form.

Spring request parameter unique within call

Massive EDIT
Let's say I have two differents controllers. One that renders the View and one that renders the <jsp:include> tags.
The ViewController :
#Controller
public class ViewController {
#Resource
private VehicleCatalogAPIService vehicleCatalogAPIService;
#RequestMapping("/en/new/{organizationUnitId}")
public String view(ModelMap modelMap, Locale locale,
#PathVariable Integer organizationUnitId,) {
Vehicles vehicles = vehicleCatalogAPIService.getVehicule(organizationUnitId);
modelMap.put("vehicles", vehicles);
return "/catalog/" + view;
}
}
The IncludeController:
#Controller
public class IncludeController{
#Resource
VehicleCatalogAPIService vehicleCatalogAPIService;
#RequestMapping(value = "/fragment/test", produces = "text/html")
public String test(ModelMap modelMap,
#RequestParam("view") String view,
#RequestParam("test") String test,
#RequestParam("vehicleId") String vehicleId,
Locale locale) {
Vehicle particularVehicle = vehicleCatalogAPIService.get(vehicleId);
modelMap.put("vehicle", vehicle);
return "/catalog/vehicle/fragments/" + view;
}
#RequestMapping(value = "/fragment/test2", produces = "text/html")
public String test(ModelMap modelMap,
#RequestParam("test") String test,
#RequestParam("view") String view,
Locale locale) {
return "/catalog/vehicle/fragments/" + view;
}
}
I hit the page in the browser : http://example.com/en/new
The ViewController is called and returns the the jsp page that has to be rendered, in this case /catalog/listing.jsp. The JSP looks like this :
<jsp:useBean id="vehicles" type="java.util.List<com.sm360.auto.webauto.webapplib.bean.display.VehicleDisplayBean>" scope="request"/>
<h1> LISTING JSP </h1>
<div>
<c:forEach items="${vehicles}" var="vehicle" begin="${k.index}" end="${k.index + k.step-1}">
<div class="c-item-out">
<content:sheet-new-vehicule vehicle="${vehicle}" isCompact="true" hasCompared="true" />
</div>
</c:forEach>
</div>
<jsp:include page="/fragment/test">
<jsp:param name="view" value="view1"/>
<jsp:param name="vehicle1" value="vehicle1"/>
<jsp:param name="test" value="test1"/>
</jsp:include>
That include now calls the IncludeController with 3 parameters : view, vehicleId and test. The proper method is invoked and the correct view is returned (view1).
The view1 contains another include :
<jsp:useBean id="vehicle" type="com.sm360.auto.webauto.webapplib.bean.display.VehicleDisplayBean" scope="request"/>
<h1> view 1 </h1>
<div>
<h2>${vehicle.name}
</div>
<jsp:include page="/fragment/test2">
<jsp:param name="view" value="view2"/>
<jsp:param name="test" value="test2"/>
</jsp:include>
In that second call, the view parameter will be "view2,view1" and the test parameter will be "test2, test1".
I would like that second include to have its own values of the parameters it passes to the controller, not a merge from the other call.
EDIT :
Following this diagram, when a <jsp:include> is met during the rendering of the view, does the process return to the Front Controller and re-Delegate the request to the Controller specified in the include tag with the same request with updated parameters?
Is it possible to have a new set of fresh parameters?
A long shot: could you just remove the view entry from the model before setting the next? I'm having a common Spring controller method in mind, please post your code for clarification.
#RequestMapping("/helloWorld")
public String helloWorld(Model model) {
model.remove("view");
model.addAttribute("view", "foobar");
return "helloWorld";
}

Spring MVC URL Path Change

I am new to web programming and Spring MVC 2.5.
I am not sure about my problem is spring sprecific or web specific in general.
I have a menu.jsp and I use jsp:include to include it in all my jsp.
<ul class="menu">
<c:url var="home" value="home.htm"/>
<c:url var="data" value="data.htm"/>
<li><span>HOME</span></li>
<li><span>DATA</span></li>
</ul>
When I hit my app the first time and calls http://localhost:8080/myapp/home.htm, everything went fine.
But when I click a link that brought me to a new url using multiactioncontroller.
#RequestMapping(value = "/epm/epmItemList.htm", method = RequestMethod.GET)
public String setupForm(Model model, HttpServletResponse response) throws IOException {
}
#RequestMapping(value = "/epm/epmdelete.htm", method = RequestMethod.GET)
public String setupForm(Model model, HttpServletResponse response) throws IOException {
}
And then I click, home again. I am getting a 404. I am not sure why, but when I check the link at my url it now it becomes:
http://localhost:8080/myapp/epm/home.htm
Spring is naturally saying no handler mapping can be found since I did not configure any mapping for these. How can I fix this problem? Thanks
Try the following change :
<ul class="menu">
<c:url var="home" value="/home.htm"/>
<c:url var="data" value="/data.htm"/>
<li><span>HOME</span></li>
<li><span>DATA</span></li>
</ul>
This way the links should be relative to your context and not relative to the local page.

Resources