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.
Related
I build a Razor View for my web app layout. In my Layout I want to get a user's photo in my layout. But, the problem is how to get the user's photo from the web api and return it to Razor View
Here's my API code:
[HttpGet("employee-photo/{employeeId}")]
public async Task<IActionResult> GetEmployeePhoto(string employeeId)
{
var employeeIdClaim = this.UtilityService.GetEmployeeIdClaim(User);
if (employeeId != employeeIdClaim)
{
return null;
}
var employeePhoto = await this.FileStorage.GetEmployeePhoto(employeeId);
var name = employeeId + ".jpg";
return File(employeePhoto, "image/jpg", name);
}
and here's my Razor View
<a class="nav-link dropdown-toggle text-white" href="#" id="userDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<img src="#Url.Action("/api/v1/blob/employee-photo/18060001")" /><span class="hidden-xs">#User.Claims.Where(Q => Q.Type == "FullName").Select(Q => Q.Value).FirstOrDefault()</span>
<i class="fa fa-caret-down"></i>
</a>
api/v1/blob is my API path, I just not write it all of the other code here
Can i get photo(image) from my API to my Razor View? How? Thanks
If you're looking for #Url.Action() extension method overloads, you will see one overload which takes string parameter like this:
public static string Action(this Microsoft.AspNetCore.Mvc.IUrlHelper helper, string action)
The action refers to actual action method name (i.e. GetEmployeePhoto), not the API URL which has been set in HttpGetAttribute. Hence, this API URL call is wrong and never reach the API controller action:
<img src="#Url.Action("/api/v1/blob/employee-photo/18060001")" />
You should use full URL string without #Url.Action() helper to make it work:
<img src="/api/v1/blob/employee-photo/18060001" />
As explained before answer, Url.Action is not right way to get photo from api in that case.
There is nothing abnormal in your GetEmployeePhoto method in my opinion. Just use direct url like:
<img src="/api/v1/blob/employee-photo/18060001" />
Still you're not getting image, just try followings:
Set a breakpoint to your method.
Request directly your api endpoint from browser.
If debugger reaches to breakpoint, check the behavior of your method
(your method should return photo as download).
If your method is not act like expected or returns error, please share it here.
If debugger not reaches to breakpoint just check your api routes.
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
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
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.
I'm trying to display success message in xhtml page from servlet but no luck, here's my code
In servlet i have
FacesContextFactory contextFactory = (FacesContextFactory)FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
LifecycleFactory lifecycleFactory = (LifecycleFactory)FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
Lifecycle lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
FacesContext facesContext = contextFactory.getFacesContext(request.getSession().getServletContext(), request, response, lifecycle);
facesContext.addMessage( "user:displaymessagesave", new FacesMessage("user saved successfully" ));
In xhtml page inside form i have written tag for display message form id=user
<h:message class="success" for="displaymessagesave" id="displaymessagesave" />
Saving to database is happening but its not displaying any message, please suggest where i'm going wrong.
It is not possible to access FacesContext from a servlet other than FacesServlet.
Recently I faced that same problem, and what I did was, on my servlet, put the messages in the Session:
List<String> msgs=(List<String>)request.getSession().getAttribute(KEY);
if (msgs==null)
{
msgs=new ArrayList<String>();
}
msgs.add("My new message");
request.getSession().setAttribute(KEY, msgs);
Then, on the xhtml view, I added a call to put these messages on FacesContext, and a growl element:
<h:outputText value="#{managed.prepareMessages()}"/>
<p:growl id="growl" showDetail="false" life="4000" />
Finally, the prepareMessages method takes them from session and add the messages to FacesContext:
public String prepareMessages()
{
List<String> msgs = (List<String>)FacesContext.getCurrentInstance()
.getExternalContext().getSessionMap().get(KEY);
if (msgs!=null)
{
for(String msg:msgs)
{
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(msg));
}
FacesContext.getCurrentInstance().getExternalContext()
.getSessionMap().remove(KEY);
}
return "";
}
It may not be an elegant solution, but is the only one I found...
Don't give the same id for message as the "for" component. If you just want a messageholder without attaching it to a valueholder than you should use the tag.
<h:messages class="success" id="displaymessagesave" />