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 .
Related
I am working on a ASP.NET Core 2.0 project using Razor Pages (not MVC).
I have the following flow:
User fills out form and POSTs
This page's POST handler validates the info and returns Page() if there are issues. If not, handler saves data to database.
From here, I want the handler to POST to a different page's POST handler with the validated and saved data from Step 2.
How do I POST to another page from within a page handler? Is this the appropriate way to do this kind of thing? The reason I don't want to RedirectToPage() is because I don't want the final page in the sequence to be navigable via GET. The final page should not be accessible via a direct link but should only return on a POST.
I thought about validating/saving the data and setting a boolean "IsValid" and returning the page, checking for that IsValid, and immediately POSTing to the final page via JS. However this feels dirty.
Set the "asp-page" property of the form to your other page. Then set values in the standard way.
<form method="post" asp-page="/pathto/otherpage">
Select Example:<select name="DataForOtherPage">
Then in your controller, bind the value...
[BindProperty]
public string DataForOtherPage { get; set; }
You don't need to cross post!
If possible, you should avoid the cross-post. Do it all under the original action. The action can return a different view by specifying the view name in the View call.
If the target of the cross-post contains complicated logic that you don't want to duplicate, extract it to a common library, and call it from both actions.
For example, instead of
ActionResult Action1()
{
if (canHandleItMyself)
{
return View("View1");
}
else
{
return //Something that posts to action2
}
}
ActionResult Action2()
{
DoSomethingComplicated1();
DoSomethingComplicated2();
DoSomethingComplicated3();
DoSomethingComplicated4();
return View("View2");
}
Do something like this:
class CommonLibrary
{
static public void DoSomethingComplicated()
{
DoSomethingComplicated1();
DoSomethingComplicated2();
DoSomethingComplicated3();
DoSomethingComplicated4();
}
}
ActionResult Action1()
{
if (canHandleItMyself)
{
return View("View1");
}
else
{
CommonLibrary.DoSomethingComplicated();
return View("View2");
}
}
ActionResult Action2()
{
CommonLibrary.DoSomethingComplicated();
return View("View2");
}
If you really want to cross-post
If you insist on using a cross-post, you will have to render a page that does the post, e.g.
<HTML>
<BODY>
<IMG Src="/Images/Spinner.gif"> <!-- so the user doesn't just see a blank page -->
<FORM name="MyForm" Action="Action2" Method="Post">
<INPUT type="hidden" Name="Argument1" Value="Foo">
<INPUT type="hidden" Name="Argument2" Value="Bar">
</FORM>
<SCRIPT type="text/javascript>
document.getElementById("MyForm").submit(); //Automatically submit
</SCRIPT>
</BODY>
</HTML>
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
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";
}
I have a controller DisplayImage which return binary file and i can see the result in its view without any
problem,
but i want use the result of this controller in image view as a partial view but it is not possible
as when i call the html.partial(_partialview) the model which have been passed to partial , is index model
let me show my code:
public ActionResult DisplayImage(int id = 1)
{
Product product = db.Product.Find(id);
return File(product.Blob, "image/png");
}
and
public ActionResult Index()
{
return View(db.Product.ToList());
}
and in index view i wrote:
#Html.Partial("_DisplayImage")
and in partial view i wrote:
<img style="width:60px" src="#Url.Action("DislpayImage", "Product", new { id="1" })" alt="myimage" />
also if i use the img tag directly in index view still doesn't work and the result is :
<img alt="myimage" src="/Product/DislpayImage/1" style="width:60px">
without any image
so what can i do with this problem
I hope describe my problem clear
If not, please ask!
Thanks in advance!
You have a typo in your action name.
DislpayImage
is not the same as
DisplayImage
<img style="width:60px" src="#Url.Action("DisplayImage", "Product", new { id="1" })" alt="myimage" />
is it possible to render a string like this:
public ActionResult Do(){
s = " hello, click here <%=Html.ActionLink(\"index\",\"index\") %> ";
return Content(RenderString(s));
}
the result would something like this:
hello, click here index
What is the purpose of this? You have a controller action which tries to evaluate some string WebForms syntax string and return it as content. Why not simply return the view and have this view do the job?
If you want dynamic to have views (coming from a database or something) you could write a custom view engine and personalize their location so that your action looks like this:
public ActionResult Do()
{
return View();
}
and the corresponding view contents will be fetched from your custom view engine instead of the standard file locations.
If you want to render the contents of a view into a string this has been covered in many blog posts. Finally if you are dealing with sending views as emails there are probably better solutions.
So depending on what you are trying to achieve there might be different solutions.
public String Do(){
string s = " hello, click <a href='" + Url.Action("Index") +"' > here </a>";
return s;
}
then if you call {Controller}/Do you will have your string
EDITED
Marco