I am rewriting a client's application from a crappy built as one huge blob of a project into a MVC application for obvious reasons.
In doing a view for pieces of it I am noticing the original programmer has plenty of statements where they change up images or put in different links based on the logged in user.
How does everybody that writes MVC applications handle this? Do you pass the username from the controller to the view to do this simple logic or should the controller handle all of that?
This should be set up either in the controller or the session, with generic place holders for the links.
i.e. if in Session
$_SESSION['userLink1']= "URL";
//Later in the views create it as this
<a href="<?php echo $_SESSION['userLink1'];">
Keep in mind this assumes that these images/links will consistently be there regardless of the user, only that the link content itself will change. If its on the user level as you described, load all the links into the session once at log in and the views will yank them out appropriately.
To actually get them in the session use your login controller to set them up upon the successful log in and when starting the session populate in some default place holders if non authorized users can also view the given pages.
Related
I've a web application that uses FreeMarker templates and tiles to do the View job in the MVC world. So upon my request to the application, say /load.do I would like to introduce an intermediate page that will have a loader icon just to indicate the page is being loaded on the subsequent request, which is not known to the user.
So ultimately what I'm achieving is a better user experience and also if used within framework (the iFrames) this comes handy to show the loader icon when there is a new request that is happening.
Can someone point me to the right method I can use here ?
This is possible using spring mvc.
You need to create something like a WaitController. On user submit , this controller will be called. This can trigger your original request in background and take a reference(UUID) for the call and will render a wait page having the reference id of the original request set in model. Wait page can either trigger immediately the main controller using reference id or can poll the reference id on regular intervals or use web socket to know the availability of results. Once results are ready, it should redirect user to the actual result page.
I have an app with 3 sections:
Main menu;
Context Menu - Related to selected item in main menu;
and Page body - Related to selected item in context menu;
"Main menu" and "Context menu" are based on membership. I don't want to load them everytime my page loads, because that would consume resources database. So, I'm using ajax to load main menu only one time, and when an item is selected, I load the context menu for that item.
My problem is: Every form's post will erase my menu.
Question: Will I have to build my entire application using ajax? I don't wanna do that, because it is too much simpler do a post in the form then send all data to controller with ajax.
Until now, I have 2 options:
Load my menus with ajax and the page body with IFRAME, so the post's will not render again my menus.
Do everything using ajax;
Is there any alternative to load my menus with ajax and be able to use form's post?
Sorry if I wasn't clear enough.
The sentence that gave me a pause is this "I don't want to load them everytime my page loads, because that would consume resources database."
You see, I've build quite a lot of apps, that display menus and sub-menus based on user roles (what you called membership). This has never been an issue from the resources or database perspective.
You can access all the membership information that you need once, when your used is being logged in. In the simplest case user's identity will be stored in the context along with the roles they have (HttpContext.User), so you do not to need a database lookup at all to get this information on every request. Note that with this scenario no ajax is required either.
If for whatever reason you can't store your membership information in the context like this, you still can store in in session (if in-memory) or in encrypted Cookies.
Now, I understand, that I don't all the details of your scenario, and that may be in your scenario what you are trying to do is warranted, however I suggest you think it through again, as under normal circumstances what you indicate is a problem (database resource) should not be a problem at all.
The bottom line is: if you alter your application that it stores the membership information when user logs on you won't have your problem to start with.
You don’t have to build all of your application using Ajax. But in this scenario Ajax may be the best way forward.
Following is my suggestion
Create your data entry for inside a dev
Have each input controller marked with a class (say ‘dataEntry’)
Create a javascript function to iterate the dev and build a list of all elements that has class dataEntry
Build a json object using the list. Use the name of each element as property name and value as the property value
Use jquery ajax to post this to the controller action
[optional] you can use .done and .fail methods to take action on success or failures of the call
I know this may look intimidating, but if you have many data entry forms, you can re-use this code.
This is our first time using this pattern (we're not even sure that this pattern is suitable for this project), and me and my mates have a couple of questions.
We're designing a simple applications for displaying files with regards to permissions.
Suppose "Joe" is currently logged in to the system, where would be the correct place to save "Joe" as the active user?
What are the roles of the controller in this context? Suppose "Joe" wants to log in to the system. He enters his password and clicks log on, should the view validate the details directly with the Model? or should it ask the controller to do it for him?
Suppose that the login is successful, what should happen now? Should the View listen as an Observer to the model and wait for a response that the login is correct and then switch windows? or should the Controller have a method called boolean ValidateLogin() which the view calls and acts accordingly?
As you can see we're very confused, any help would be appreciated.
Session / Database / Configuration File (is this a single or multiple user system?)
In some web frameworks*: the view passes the login data to controller, passed to model which would validate, return the result to controller, and give the correct view as response. Surely it's also fine if the view wants to directly validate the data to the model, but IMO view-model communication should never have any logic involved, only data update notification.
I think it's partially answered in answer 2 above. The controller should be able to destroy/change the login view though (when the login successful, normally the view should change accordingly).
*This is because in web application, usually view cannot directly talk to model, as opposed in desktop application. This is still an MVC, only a variation called MVA.
For your requirements, I suggest you use this project MembershipStarterKit to start with.
It has samples that do basic authentifacation and role management.
It also use the .Net membership provider so that you don't re-invent the wheel.
Store it in session
Any client side validation can be done by the view directly with the model(Through DataAnnotations). And for database calls it goes through the contoller
On successful login, controller will render the corresponding view
I am new to JSP/Servlets/MVC and am writing a JSP page (using Servlets and MVC pattern) that displays information about recipies, and want the ability for users to "comment" on it too.
So for the Servlet, on doGet(), it grabs all the required info into a Model POJO and forwards the request on to a JSP View for rendering. That is working just fine.
I'd like the "comment" part to be a separate JSP, so on the RecipeView.jsp I can use to separate these views out. So I've made that, but am now a little stuck. The form in the CommentOnRecipe.jsp posts to a CommentAction servlet that handles the recording of the comment just fine. So when I reload the Recipe page, I can see the comment I just made.
I'd like to:
Reload the page automatically after commenting (no AJAX for now)
Block the user from making more than one comment on each Recipe over a 1 day timeframe (via a Cookie). So I store a cookie indicating the product ID whenever the user makes a comment, so we can check this later? How would it work in a MVC context?
Show a message to the user that they have already commented on the Recipe when they visit one
which they have commented on
I'm confused about using beans/including JSPs etc on how to achieve this.
I know in ASP.NET land, it would be a UseControl that I would place on a page, or in ASP.NET MVC, it would be a PartialView of some sort. I'm just confused with the way this works in a JSP/Servlets/MVC context.
you can use response.sendRedirect() or forward APIs in javax.servlet to redirect to a new page or refresh the same page (redirect to the same page/path so that the beans/data gets refreshed)
about restricting to one comment per day - yes you can use cookie but the problem is that user might use another browser type (chrome, FF, Safari) and will be able to make multiple comments.
Ideally you should store the lastCommentTime in the model/persistent store and tie it to the user information - this way your model object can expose an API that checks the last comment time and returns true/false depending on whether user can comment or not.
You can use this API in your servlet/JSP to show/hide the comment button, for example and also show a message
I am aiming to create a payment module. Its users shall be redirected away from the site's URL in order for the transaction to be processed by a third party at a different URL. I would then like customers to be redirected back to a generic 'success' page that notifies them the order was a success. I have tried redirecting to the default success page (checkout.thankyou.php), but I get lots of errors; all the constants etc. that the application requires have obviously been lost during the redirect.
I would like to be able to retrieve the theme currently enabled in the configuration and use it to insert some basic HTML into the view. I would also like to access the database to perform some queries.
Can anybody advise? I am very stuck, and cannot find anything useful in the documentation! Thank you.
Can you be more specific about what type of information you want in your success page? If you just want basic HTML, then there's no reason you can't just write a basic Joomla article and redirect to that instead of trying to redirect to a VM partial. Again, if it's just basic HTML (no data from the transaction), then you can simply use a code inspector (like FireFox Inspect Element) to track down the CSS classes you like from the template and simply use them in your Joomla article to make it look like the VM template. You can find most of them in components/com_virtuemart/themes/default/themes.css.
If you need to display actual transaction data in your Thank You message, be prepared for a bit more work. You're probably going to have to write a cookie containing the record data BEFORE it gets sent offsite, and then read the cookie just prior to rendering the Thank You page.