angularjs route with spring framework - spring-mvc

I've developed a simple web page with angularjs and spring framework.
I tried to make the web page using angular route so that web page work as SPA.
Here's my simple main jsp file. The page is shown when I access to 'http://localhost:8080/test' on the chrome.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# page session="false" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-route.min.js"></script>
<script>
angular.module("app", ['ngRoute'])
.config(function($routeProvider) {
$routeProvider.when("/home", {
templateUrl: "home.html",
controller: "controller"
})
})
.controller("controller", function() {
});
</script>
</head>
<body ng-app="app" ng-controller="controller as main">
<ul>
<li>HOME</li>
</ul>
<ng-view></ng-view>
<div ng-view></div>
</body>
</html>
I want to put the home.html into <ng-view> or <div class="ng-view"> when I click the 'HOME' link without page refresh but it's not working.
What's the problem with the code?

To get it to work change href="#/home" to href="#!home"
Also, make sure home.html is in the /src/main/resources/static/ directory
I would prefer to use .html with Thymeleaf instead of .jsp
Read
Integrating JSP with AngularJS, Is it a concern in real world...Beginer in Angular JS
https://spring.io/blog/2015/08/19/migrating-a-spring-web-mvc-application-from-jsp-to-angularjs

Related

Layout page with downloaded layout template not rendering properly

I created a new ASP .Net MVC 5 project with all the sample views, controllers and such. It has the latest versions of jQuery and Bootstrap.
So I wanted to have a nice template on it so I downloaded this one and I added the images and css files to the content folder, modified the BundleConfig class so that the new css file is included and also changed _ViewStart.cshtml file so that it takes the new layout which I named _myLayout.cshtml.
Technically it worked very well since all the pages now use the new layout template but it is not rendering properly, the menu looks truncated, the header image and the content section are going outside of the border on the right side, and the footer is displayed with a margin at the bottom which should not exist.
I have been able to pinpoint the problem to a "collision" between the template's css and bootstrap since if I remove the bootstrap stylesheet from the BundleConfig file and then styles got rendered as expected.
I am more of a backend/javascript developer so my knowledge about css is very limited and I am way over my head with this one. So much that I don't even know which code to include and both css files are too large to include them in the question.
If anyone is able to help I would really appreciate it and/or if someone can provide some clues as to which css sections would be useful to include in the post to help pinpoint the problem please let me know.
Thanks in advance.
UPDATE:
To replicate the problem you would have to:
- Download the template I mentioned above
- Create a new ASP .Net MVC 5 project
- Add the images and styles.css file from the template on the project's content folder
- On the project's Shared folder create a new layout page called _myLayout.cshtml
- Do the following changes on these files:
BundleConfig.cs
using System.Web;
using System.Web.Optimization;
namespace SC.Web
{
public class BundleConfig
{
// For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.validate*"));
// Use the development version of Modernizr to develop with and learn from. Then, when you're
// ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
"~/Scripts/modernizr-*"));
bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
"~/Scripts/bootstrap.js",
"~/Scripts/respond.js"));
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/site.css",
"~/Content/styles.css"));
}
}
}
_ViewStart.cshtml
#{
Layout = "~/Views/Shared/_myLayout.cshtml";
}
_myLayout.cshtml
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>#ViewBag.Title - My ASP.NET Application</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
#Styles.Render("~/Content/css")
#Scripts.Render("~/bundles/modernizr")
</head>
<body>
<div id="main">
<!-- start header -->
<div id="header">
<div id="logo">
<h1>metamorph_highway</h1>
<h2>Design by Metamorphosis Design</h2>
</div>
<div id="menu">
<ul>
<li>#Html.ActionLink("Home", "Index", "Home")</li>
<li>#Html.ActionLink("About", "About", "Home")</li>
<li>#Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
</div>
<!-- end header -->
</div>
<hr />
<!-- start page -->
<div id="page">
<!-- start content -->
<div id="content">
#RenderBody()
</div>
<!-- end content -->
<div style="clear: both;"> </div>
</div>
<!-- end page -->
<hr />
<!-- start footer -->
<div id="footer">
<p>© #DateTime.Now.Year - My ASP.NET Application</p>
<p>Copyright © 2008. Privacy Policy | Terms of Use | <abbr title="eXtensible HyperText Markup Language">XHTML</abbr> | <abbr title="Cascading Style Sheets">CSS</abbr></p>
<p>
Design by Free Site Templates, coded by Free Flash Templates
</p>
</div>
</div>
<!-- end footer -->
#Scripts.Render("~/bundles/jquery")
#Scripts.Render("~/bundles/bootstrap")
#RenderSection("scripts", required: false)
</body>
</html>
Turns out that on bootstrap there is this definition:
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
Since the template I downloaded gets messed up with this I added the following on its own css file:
*{
-webkit-box-sizing: content-box;
-moz-box-sizing: content-box;
box-sizing: content-box;
}
I have no idea if later this will cause bootstrap not to behave as it should and if that happens I'll have to deal with that then.

Refresh RenderBody() Content with Razor Pages

I've got an ASP.NET project using MVC 4 and Entity Framework with the following structure:
-Controllers
-HomeController.cs
-SystemInformationController.cs
-Views
-Home
-Index.cshtml
-Shared
-MasterLayout.cshtml
-SystemInformation
-Index.cshtml
The MasterLayout.cshtml code is as follows:
<!DOCTYPE html>
<html>
<head>
<title>Sample Site</title>
<link href="~/Content/MasterPage.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="TopContent">
<a>Home</a>
</div>
<div id="LeftContent">
#Html.ActionLink("Home", "Index", "Home", new { #class = "basiclink" })
<br />
#Html.ActionLink("System Information", "Index", "SystemInformation", new { #class = "basiclink" })
<br />
</div>
<div id="MainContent">
#RenderBody()
</div>
The Index.cshtml code for the Home view is as follows:
#{
Layout = "~/Views/Shared/MasterLayout.cshtml";
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Home Page</title>
</head>
<body>
<div>
Welcome to the Sample Site.
</div>
</body>
</html>
The problem I am having is that whenever I click on either link, be it the home link or the system information link, the url simply appends a "?length=X" where X is the length of the third parameter in the ActionLink item. I want to have the content held within the Master Page to be refreshed but no matter what I do the URL seems to be built incorrectly and not refreshing the center content. Anyone have any ideas as to why that is happening?
The problem is you are using a different overload of the ActionLink method than you probably want. The one you are using is taking your third parameter and using it as the route values, not the controller name. Try changing your links to this:
#Html.ActionLink("Home", "Index", "Home", new {}, new { #class = "basiclink" })

How do I use #Url.Content() in non-MVC Razor webpage?

Since I'm using Web Application having NuGet Package's Microsoft.AspNet.Razor installed.
In WebForm pages (.aspx), I can use ResolveUrl() but in Razor pages (.cshtml), I get this error -->
"x:\Source\Foo.Dealer\Foo.Dealer.WebApp.Mobile\Member\DmsDashboard.cshtml(103): error CS0103: The name 'Url' does not exist in the current context"
Source-Code here..
#section HeadJavascriptLibraryFile
{
<script type="text/javascript" src="#Url.Content("~/scripts/webpages/setting-dmsdashboard.js")"></script>
}
and
<img src="#(Url.Content("~/images/miscellaneous/reportandpiechart2.png"))" alt="" />
Source Code as requested...
//LayoutMembmerGeneral.cshtml
#using Foo.Dealer.WebApp.Mobile.Infrastructure;
#{
if (LoginManagementTools.DealerUserLoginValidation_BrowsingPage(HttpContext.Current.Request, HttpContext.Current.Response, HttpContext.Current.Session) == false) { }
}<!DOCTYPE html>
<html>
<head>
<title>#Page.Title</title>
#RenderSection("HeadJavascriptLibraryFile", false)
</head>
<body>
#RenderBody()
</body>
</html>
//DmsDashboard.cshtml...
#using Foo.Dealer.WebApp.Mobile.Infrastructure
#using System.Web.Mvc;
#{
Page.Title = "A New Dawn In Auto Pricing";
Layout = "LayoutMemberGeneral.cshtml";
}
#section HeadJavascriptLibraryFile
{
}
<div id="WebLayout1">
<img src="#Url.Content("images/miscellaneous/reportandpiechart2.png")" alt="" />
</div>
The URL helper doesn't seem to exist in ASP.Net Web Pages (which is essentially what you are attempting to do), but you can simply use the following code to achieve the same effect:
<img src="~/images/miscellaneous/reportandpiechart2.png" alt="" />
The tilde (~) references the application root and is transformed when the page is compiled and sent to the client.
The tilde (~) is not transformed as part of any data- attributes and likely, other places also, though I haven't researched extensively.
If you happen to need this information in a place where the automatic transofmration isn't working, you can use the tilde equivalent, HttpContext.Current.Request.ApplicationPath.
In most Razor views, you should be able to shorten it to the following:
#Request.ApplicationPath
Reference: http://msdn.microsoft.com/en-us/library/system.web.httprequest.applicationpath.aspx
Another alternative in some situations is to use the VirtualPathUtility such as:
#VirtualPathUtility.ToAbsolute("~/")

how to set css for html page opened by 'appAPI.openURL' in crossrider

I'm creating extension using crossrider. In this extension I want to open a new tab with html from resources. Its opening page in new tab without issues. Now I want to add js & css to that, that to available in resources. Kindly help in adding css & js.
code in background.js
appAPI.openURL({
resourcePath: "troubleShooter.html",
where: "tab",
focus: true
});
in troubleShooter.html
<html>
<head>
<link media="all" rel="stylesheet" type="text/css" href="css/ie.css" />
<script type="text/javascript" src="js/ie.js"></script>
</head>
<body>
</body>
</html>
Crossrider recently introduced the ability to open a new tab with HTML from resources. However, such pages cannot directly access other resource file using link and script tags embedded in the HTML.
Though in it's early release, one of the features of the HTML page is the crossriderMain function that runs once the page is ready. In this early release, the function supports the following Crossrider APIs: appAPI.db.async, appAPI.message, and appAPI.request.
Hence, even though in this early release there isn't a direct method to add resource CSS & script files to the resource HTML page, you can workaround the issue by loading the resources into the asynchronous local database and applying it to the HTML page using standard jQuery. For example:
background.js:
appAPI.ready(function() {
// load resource file 'style.css' in to local database
appAPI.db.async.set('style-css', appAPI.resources.get('style.css'));
// load resource file 'script.js' in to local database
appAPI.db.async.set('script-js', appAPI.resources.get('script.js'));
// open resource html
appAPI.openURL({
resourcePath: "troubleShooter.html",
where: "tab",
focus: true
});
});
troubleShooter.html:
<!DOCTYPE html>
<html>
<head>
<!-- This meta tag is relevant only for IE -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script type="text/javascript">
function crossriderMain($) {
appAPI.db.async.get('style-css', function(rules) {
$('<style type="text/css">').text(rules).appendTo('head');
});
appAPI.db.async.get('script-js', function(code) {
// runs in the context of the extension
$.globalEval(code.replace('CONTEXT','EXTN'));
// Alternatively, run in context of page DOM
$('<script type="text/javascript">')
.html(code.replace('CONTEXT','PAGE DOM')).appendTo('head');
});
}
</script>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
style.css:
h1 {color:red;}
script.js
console.log('CONTEXT:: script.js running');
Disclaimer: I am a Crossrider employee

ASP.NET MVC Dynamic Stylesheets

The project I'm working on allows an end-user to modify CSS code to integrate the application as best possible. Most of the CSS values are stored in a database and need to be retrieved and parsed dynamically.
I setup a Style controller, and gave each stylesheet an action, and then passed the configuration data through to the View. I set the ContentType to "text/css" and then generated the stylesheets.
This works fine, but the problem I'm running into is this: none of the code works in the application. I include it in the head code, but it doesn't parse in the code.
An example of what I do is this:
<%# Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" ContentType="text/css" %>
.element {
background-color: <%= ViewData.Model.BackgroundColor %>;
}
I include it like so:
<link href="/style/Basic" rel="stylesheet" type="text/css" media="all" />
When I include the CSS code in a partial view and include it using the ViewModel (wrapped in style tags) in an action, then everything works fine. It is when I try to parse this as a separate file when it does not work.
Is there something I am doing incorrectly? Or is there some kind of glitch?
Thanks in advance :D
Use a tool such as HTTPWatch to verify that the stylesheet is being sent down and not 404'd
Controller
public ActionResult Basic()
{
Response.ContentType = "text/css";
var basicVM = new BasicVM()
{
BackgroundColor = "Lime",
};
return View(basicVM);
}
And the View
<%# Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcApplication3.Controllers.BasicVM>" ContentType="text/css" %>
body {
background-color: <%= ViewData.Model.BackgroundColor %>;
}
and the test page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Test</title>
<link href="/Home/Basic" rel="stylesheet" type="text/css" media="all" />
</head>
<body>
<div>
Test
</div>
</body>
</html>
Turns everything Green

Resources