my project structure :
RouteConfig :
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
index.html :
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<h3>hi</h3>
</body>
</html>
Home Controller :
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
WebApi is startup project.
how can display index.html in Index action or replace Index view with index.html.
The files are in different profects, you canĀ“t do it at this way.
Why you dont use the home/index action and views/home/index.cshtml to put your index.html content?
Or you can make a redirect to index.html URL on home/index action.
By default, your controller action will return Index.cshtml view instead of Index.html page. If you want to do redirection pointed at Index.html on WebClient project, modify your Index action method inside controller like this (set your WebClient's full address path, because tidle sign to replace site root path doesn't work at this case):
public class HomeController : Controller
{
public ActionResult Index()
{
return Redirect("http://ServerAddressOrName:Port/WebClient/Index.html");
}
}
Afterwards, you need to build both projects then assign your WebApi and WebClient to different sites, since it is impossible to combine them with different Web.config content.
Related
I am working on a page where I have to retrieve the "tasks" of a specific date from the database. My current approach is to use GetMapping at the server, and return the list of tasks
Below is part of my TaskController
#Controller
#RequestMapping()
public class TaskController {
#Autowired
private TaskService taskService;
#GetMapping("/calendar/{date}")
public String displayTasksByClick(#PathVariable("date") int date, Model model) {
long userId = this.getCurrentUserId(); // just a method to get the user id requesting the task
List<Task> taskList = taskService.findByDateAndUserId(date, userId);
model.addAttribute("taskList", taskList);
return "/calendar";
}
And calendar.html looks like this (I'm only pasting the relevant part)
<html lang='en' xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset='utf-8' />
<title>Dashboard</title>
<link rel="stylesheet" href="../static/css/calendar.css">
<script src="jquery-3.5.1.min.js"></script>
</head>
<body>
<ul style="list-style-type: none; margin: 0;">
<li><a th:href="#{/calendar/20220228}">show</a></li>
<li><a th:href="#{/calendar/20220301}">show</a></li>
<li><a th:href="#{/calendar/20220301}">show</a></li>
</ul>
......
<div th:each="task : ${taskList}">
<label th:text="${task.name}"></label>
</div>
<!-- the rest is irrelevant to the question --!>
.......
</html>
So whenever I click on the <a> elements, the client sends a request to the server, and the URL is handled by GetMapping method, returning the tasks. But while this happens, the page is also refreshed. Is there a way to display the tasks without having to refresh the page?
I tried returning void from the display method, but Spring ends up automatically returning /calendar/{date}, and it's still not what I want
#GetMapping("/calendar/{date}")
public void displayTasksByClick(#PathVariable("date") int date, Model model) {
long userId = this.getCurrentUserId(); // just a method to get the user id requesting the task
List<Task> taskList = taskService.findByDateAndUserId(date, userId);
model.addAttribute("taskList", taskList);
}
For your current implementation, no, it is not possible. You have to refresh the page for the tasks to be displayed. That's how server side rendering works. The page is created on the server with the dynamic data, then the static page is returned to the browser.
I will show you my code and project structure then what I tried so far:
WebMvcConfigurer:
#Configuration
public class ResourceHandlers implements WebMvcConfigurer {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/*").addResourceLocations("/resources/css/");
}
}
This is in my .jsp page: <spring:url value="/resources/css/homepage.css" var="homePageCss" />
This is my project strucutre:
.idea
.lib
-src
-main
.java
- resources //this is automatically created folder resources
- css
- homepage
.homepage.css
-webapp
- resources
- css
- homepage
.homepage.css
In this project structure that I showed I tried both ways. Manually create resource folder inside webapp and then create folder css, homepage, and then css page.
Also I tried to create css, homepage and the css page inside my automatically created folder resources but both ways doesnt work.
Also add #EnableWebMvc Annotation
Try This
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/css/homepage/");
}
JSP
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%> //Add at the top the page
<%#page isELIgnored="false"%> //Add at the top the page
<spring:url value="/resources/homepage.css" var="homePageCss" />
<link href="${homePageCss}" rel="stylesheet" />
I am trying to use the KoolReport in laravel vuejs but it shows me the blank page any help will be highly appreciated.
Code in MyReport.php is like below
<?php
namespace App\Reports;
class MyReport extends \koolreport\KoolReport
{
//We leave this blank to demo only
Code in MyReport.view.php is like below
<html>
<head>
<title>My Report</title>
</head>
<body>
<h1>It works</h1>
</body>
</html>
Code in ReportController.php is like below
<?php
namespace App\Http\Controllers;
use App\Reports\MyReport;
class ReportController extends Controller
{
public function __contruct()
{
$this->middleware("guest");
}
public function index()
{
$report = new MyReport;
$report->run();
return view("report",["report"=>$report]);
}
}
Code in report.blade.php is like below
{{ $report->render() }}
Code in ReportController#index is like below
Route::get('/report', "ReportController#index");
I have a simple problem which I couldn't solve until now, the problem is when the user asks for rest password, the email is sent correctly except one thing that the email, doesn't contain a Subject. And I want to add the subject but I wasn't able to do it.
here is the postRemind function in my controller:
public function postRemind()
{
$this->reminderForm->validate(Input::only('email'));
switch ($response = Password::remind(Input::only('email'))) {
case Password::INVALID_USER:
return Redirect::back()->with('error', Lang::get($response));
case Password::REMINDER_SENT:
return Redirect::back()->with('status', Lang::get($response));
}
}
and here is my blade :
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h3>Password Reset</h3>
<div>
You have requested password reset. Complete this form: {{ URL::to('password/reset', array($token)) }}
</div>
</body>
</html>
You can pass a closure to Password::remind where you can set the subject.
https://laravel.com/docs/4.2/security#password-reminders-and-reset
public function postRemind()
{
$this->reminderForm->validate(Input::only('email'));
$response = Password::remind(Input::only('email'), function($message)
{
$message->subject('Password Reminder');
});
switch ($response) {
case Password::INVALID_USER:
return Redirect::back()->with('error', Lang::get($response));
case Password::REMINDER_SENT:
return Redirect::back()->with('status', Lang::get($response));
}
}
This probably a very simple question, but I can't figure it out alone. I have the following controller:
#RestController
public class TestController extends AbstractCloudController {
private final EquipmentRepository equipmentRepository;
#Autowired
TestController(EquipmentRepository er) {
equipmentRepository = er;
}
#RequestMapping(method=RequestMethod.GET) void index() {
List<String> codes = equipmentRepository.findAllEquipments();
String output = "";
for (String code : codes) {
output += "ID "+code+"\n";
}
}
}
And the following index.html:
<!doctype html>
<html>
<head>
<title>Test Page</title>
<link rel="stylesheet"
href="./bower_components/bootstrap-css-only/css/bootstrap.min.css" />
</head>
<body ng-app="testApp">
<div class="outer">
<h1>Hello World!</h1>
<div class="container">
<div ng-controller="TestController" ng-cloak="ng-cloak">
<p>This is a test page</p>
</div>
</div>
<script src="js/angular-bootstrap.js" type="text/javascript"></script>
<script src="js/hello.js"></script>
</div>
</body>
</html>
How do I get the information from the controller to the server side without it overriding the html? When I make the controller return something, it ends up overwriting the html and printing only the equipment code instead instead of the "Hello World", even the page title doesn't show.
I don't know what do you returned in your controller, but you may check this document: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-return-types
If you want to print the content of output, you may add it to a Model
#RequestMapping(method=RequestMethod.GET, Model model) void index() {
List<String> codes = equipmentRepository.findAllEquipments();
String output = "";
for (String code : codes) {
output += "ID "+code+"\n";
}
model.addAttribute("output", output);
}
then use JSP Expression Language to get the content
${output}
PS. Spring will treat a returned string as view name, not html content by default.