how to redirect to home page from Servlet to JSP with Angular UI - angularjs

I have an AngularJs UI login page and I'm posting the username and password details to a Servlet to validate, and when I'm returning back I'm using sendRedirect from Servlet but it's not working. Here is my sample code
In Servlet:
req.sendRedirect("NewFile.jsp");
It is displaying content inside NewFile.jsp within the login page itself instead of redirecting to NewFile.jsp
I have tried using "$window.location.href" inside AngularJs code but that itself didnt forward to home page.
Any help is much appreciated.
Thank you!

Acording of what you said in the comment of the bounty, I have to say that having everything in only one controller sounds like a bad idea, for more information about this and naming conventions go to Naming conventions and best practices
According to your question, maybe you need to give more details of the problem, it's not clear.
Have you tried to use $location module?
https://docs.angularjs.org/api/ng/service/$location
You can use de .path() module to redirect.
$location.path('#!/');
That will redirect you to the home page.

Related

Dealing with %23 replacing id # in angular routing.

The problem:
I have a express backend/angularjs frontend web app hosting all of my teams documentation for our users. We frequently need to post links to a specific question (all linked as ids) to our users in Slack. ex. helpapp.com/productTheyNeedHelpWith#QuestionLinkedTo the problem is that when the link is clicked from slack, not when copy/pasted, Slack launches a new browser window/tab to helpapp.com/productTheyNeedHelpWith%23QuestionLinkedTo.
This breaks ngRoute as it no longer recognizes /productTheyNeedHelpWith as the path to load the correct page for and the user gets sent to our home page instead.
Thoughts on possible solutions:
I'd like to solve this issue either in the ngRouting itself by somehow updating the path by replacing %23 with # before $routeProvider does it's magic, or by somehow capturing the full url in app.js and updating it or redirect to an updated version before we've rendered a page view at all.
currently I've set a workaround by using a ng-init event to check the url and, if %23 is found, update it and replace it with the correct extension but this causes an odd double load that leads to a poor user experience.
Final Thoughts:
As I'm not sure of the best way to go about this, and because our code is proprietary, I'm avoiding posting to much 'example code' that may not be relevant but am happy to provide any parts of it that you, wonderful stackoverflowers, may request. Thank you in advance for your assistance.

How to share(facebook or twitter) a page in AngularJS depend of the route?

I have a problem with sharing a page in AngularJS, when I click to "Facebook Share button", just took the Open Graph of the home, but not the data of the page that I am.
Please help me =)
I don't know what component are you using now, but this one seems quite easy to use and understand, with examples included:
https://github.com/djds4rce/angular-socialshare

AngularJS instant URL loading

this question may sound noob to geeks with ng-expert tag in their shirts.
I've been seeing some AngularJS-based sites that whenever I clicked a link, the URL changes but the browser does not load, it only loads dynamic content to their ng-view.
If current URL is http://www.sample.com/ and I clicked on Contact us link, the URL at my browser becomes http://www.sample.com/contact-us and so the page content changes like Ajax load but the browser does not load.
That thing is kind of new to me, can someone enlighten me about this? I've seen several tutorials but links are only hashtag-based (http://www.sample.com/#contactus) and not route based.
I don't know what specific Angular concept it is so I don't know what to look for.
I'll greatly appreciate constructive solid answers. Thanks!
You can do routing with angular js without hash tags, by setting
// use the HTML5 History API
$locationProvider.html5Mode(true);
so your browser url becomes http://www.sample.com/contact-us not http://www.sample.com/#contact-us
here is the complete tutorial blog,
https://scotch.io/quick-tips/pretty-urls-in-angularjs-removing-the-hashtag
You can change pages dynamically by changing the HTML on the main site or by
$routeprovider
URL rewriting has to be supported by the server the site is hosted on.
For this, look at: $location

angularjs technique for login then redirecting to dashboard page

I'am doing my first AngularJS project with ASP.NET Web API backend. What I am trying to do is, whenever a user visits www.mydomain.com, a login page (index.html) will displayed. After successfull login, he will be redirected to the dashboard.html (this is the shell page, partial views go here). My project structure is shown below-
I am confused about some issues:
Is this the best/common practices what i am trying to do in above?
As because dashboard.html is the main page, should i place app.js on dashboard.html?
If i put app.js on dashboard.html, will index.html (login page) have another app.js (i.e. loginApp.js)?
How should I manage the login state i.e. IsUserLoggedId, UserId etc in angular part?
This question may be silly. I googled, but did not find any example/article addressing such issue.
Would you please help?
Thank you in advance.
I am not sure how ASP.NET deals with it, but to my knowledge ASP.NET is just a server side framework whereas AngularJS is client side framework.
These two framework solve different problem, but has some overlapping features.
If you start using angularjs, then most of the time you will deal with the term "Single Page Application (SPA)".
There are different approaches in how you can handle the url redirection after login. I will just show you two example. There are many more how you can handle the user authentication and session.
First Approach:
In SPA, most of the time browser will change the url route and state directly in the page itself without making the entire page request from the server.
With that said, your dashboard.html will most likely be come a static template file which will be loaded from the browser directly. (i.e. the server does not dynamically parse the dashboard.html but only serve as a static file). Upon the user login, the angularjs will fire a asynchronous HTTP request into the ASP.NET authentication end point. A successful login may return a token to the browser, and the client will use it to manage the user session. At the same time, the Angular will have to change the route to /dashboard/. Notice that that the entire flow happens transparent to the user, it does not fire a full page HTTP request.
Second Approach:
Alternatively, if you choose to redirect from the server, you will have to send a HTTP Redirect 302. and since HTTP redirect will eventually call make a full HTTP request to /dashboard/, and it will then have to reload and bootstrap the angular app.js from the browser again. In this case, the user will have to wait for the dashboard page to be processed by the server upon login
Issues:
Is this the best/common practices what i am trying to do in above? there are many approaches, I think it is best to find the one that works for you. If you have a RESTful API, then you might want to have a look at the SPA approach in more detail.
As because dashboard.html is the main page, should i place app.js on dashboard.html? in SPA, you don't need to load app.js twice. but if you use the second approach, you have to reload the app.js again.
If i put app.js on dashboard.html, will index.html (login page) have another app.js (i.e. loginApp.js)? depends on your approach as stated above
How should I manage the login state i.e. IsUserLoggedId, UserId etc in angular part? Authentication Strategy, UNIX style authorization
There are more official guide that can help AngularJS Developer Guide.
Hope this helps you to integrate with the ASP.NET authentication mechanism.
you should have multiple shell pages. this link can help you...
refer to Multiple Shell Pages part.

How to find current controller or action in routes.php in cakephp

I try lots of methods to find current controller and action in cakephp routing, but I didn't got answer. Is there any method to find controller and action in routing. If yes, please help me. Or if there is any other way to find these, Please tell me.
It's the routes that decide to what controller and action a url maps to. When routes.php is being loaded the routing process has still not been done (since you are still setting up routes), so you cannot know controller or action is in routes.php.
If the website formatted something like this :
www.johndoe.test/test/test_action/
then probably it can be found on test controller but of course that depends on the settings of the website on how the developer configured it. It may also be found on routes.php like #ADmad said.

Resources