How to map root of Web App in Stripes to an ActionBean - url

I'm using DynamicMappingFilter and annotated URLs for all my actions.
I'm trying to map "/" to an existing actionBean. The actionbean I want "/" to go to, is currently bound to "/categories".
I found two ways, but I'm wondering if there is a better way in terms of performance.
I created a new IndexActionBean with #UrlBinding("/") at the top.
Inside of it, I can return a ForwardResolution to the Categories class, or I can copy paste the category class code in it, since mine is very simple.
#UrlBinding("/")
public class IndexActionBean extends AbstractActionBean {
#DefaultHandler
public ForwardResolution view() {
return new ForwardResolution(ShowCategoryActionBean.class);
}
}
It works, but I don't like having the overhead of the ForwardResolution, especially since this is the root page of the domain and will get a lot of pageviews.
I've tried to use the welcome file, but it doesn't work with DynamicMappingFilter. It's working with DispatcherServlet, such as category.action, but even then, I had problems, and only / was working, and /category and all other urls stopped working and giving 404 not found.
<welcome-file-list>
<welcome-file>category.action</welcome-file>
</welcome-file-list>
Any better ways? I cannot just rename #UrlBinding of /categories to / because I still need /categories, but I want / to forward to it as well.

In your IndexActionBean extend CategoriesBean rather than AbstractActionBean that is:
#UrlBinding("/")
public class IndexActionBean extends CategoriesBean {
}
So that you can have two url's mapped to same action bean.

Use /index.jsp to forward to your action.

Related

Conditional namespaces in mvc views

I'm working on MVC 3 and using resource files to localize the application. Now we have another customer on board for the application and they would like to change some of the text on the application..typical.
I have create a separated resource file for them and would like to do something like this in views
if (customer =A )
#using Resources.customerA
else
#using Resources.customerB
I've a resource class in both namespaces so something like this works fine if I change the namespace
Resource.WelcomeUser
Is it possible to use conditional using statement in views? I'm unable to find the right syntax for this. Any ideas?
You can put both using statements in View, but when you use classes you would have to write some namespace prefix.
Example:
#using Project.Resources.customerA
#using Project.Resources.customerB
using classes:
customerA.WelcomeUser
customerB.WelcomeUser
I think there is no other way, because two files cannot have the same path.
What you're really talking about is the provider pattern. You have two (or more) interchangeable things, and you want to be able to use one or the other contextually.
The correct way to do this in an OO context is to create and use an interface, while then injecting the actual implementation you want at runtime. You can actually achieve this in ASP.NET Core, which supports injection in Views, but in ASP.NET MVC 5 and previous, you'd need to go a little out of your way. I'm imagining these are currently static classes, since you're referencing them merely via namespace. With that approach, you'd need to follow #Ssheverdin's advice and use the FQN of the class (i.e. with the namespace):
#if (customer == A)
{
#Resources.customerA.StaticClass.Property
}
else
{
#Resources.customerB.StaticClass.Property
}
Alternatively, you could change the static classes to be instance classes and use a factory pattern to return the right one. This is a very simplistic example, but hopefully enough to convey the idea:
public static class ResourceFactory
{
public static IResourceClass GetForCustomer(string customer)
{
switch (customer)
{
case "A":
return new Resources.customerA.ResourceClass();
default:
return new Resources.customerB.ResourceClass();
}
}
Then:
#{ var resource = ResourceFactory.GetForCustomer(customer); }
I have managed to achieve the behaviour by adding a web.config file under views folder and including the namespaces there, i have to remove the #using statement from all views obviously. You might find that intellisense doesn't work anymore for you so try closing all views and reopen them again.
With this way I can create a separate web.config file for each customer and specify the relevant namespaces accordingly. Now just have to make sure to provide the RIGHT config file for each customer when deploying the release:)

Create two JpaRepository for same entity, one for internal use, the other for rest operations

I would like to create an entity that is exposed with spring data rest and secured. Example :
#PreAuthorize("denyAll")
#RepositoryRestResource(path = "exposed-users")
public UserJpaRepository extends JpaRepository<Long, User> {
#Override
#PreAuthorize("hasRole('ADMIN')")
Page<User> findOne(#Param("id") Long id);
}
This is maybe a bit silly (but just for the example and a bit more complicated in my case) : only the /exposed-users/{id} should be read accessible (via GET), and everything else should be denied.
The problem is that this makes the repository unusable for the rest of the application, for example a userJpaRepository.count() will fail because of the #PreAuthorize("denyAll").
So I thought why not create two JpaRepositories for the same entity :
the one above, for external REST access, secured
another one for all internal uses, not secured, with no rest annotation, like this :
public InternalUserJpaRepository extends JpaRepository {
}
I can't get this to work though ... more precisely it works randomly, sometimes the Users are exposed with no security on the default /users mapping (so the InternalUserJpaRepository is used), sometimes the Users are exposed correctly on the /exposed-users mapping, with proper security.
I'd like to remove the random factor :-)
Is there a way to properly do this ?
I already thought of ditching the method security altogether and go with a WebSecurityConfigurerAdapter (with ant patterns et al) but if I would prefer staying with method security if possible.

Is it legal to extend an entity model with functionality in ASP.NET MVC

first of all here is my situation. I am programming an intranet application using ASP.NET MVC 3 with Entity Framework 4.1. My application has been developed using the "Unit of Work" and "Repository" design patterns.
How ever in my opinion it should go the way that my application has an unit of work that provides a central access to all the repositories which further provide access to the entities.
Lets say I have a entity called "ProductApprovalDocument" with the properties "id", "creationDate" and "approvalDecission" stored in the database. Now I want the user to be able to access a PDF file of the document thats shortly described by the entity. Because the files are stored in a central directory on a file server using the URL format "[fileServerDirectoryPath]/[ProductApprovalDocument.id].pdf", I do not want to save an extra property for that filepath on the database. What I would like to do, is give the entity an extra property called "filepath" that automatically constructs the path with the given information and returns it.
Now the Problem:
I use an interface called FileService to abstract file access from the rest of the application. Now in my case I would have to access the UnitOfWork object out of the entity model, to retrieve the current FileService implementetion and get the preconfigured filepath. I think that's the totaly wrong way because to me an entity model should only be used as a data container not more or less.
Now the Question:
How do I handle such a situation. I would not like to always set the filepath property through the controller because ist more or less static and therefore could be done somehow automatic by the model.
Edit (final solution):
Thanks to the answer of Andre Loker I gained another point of view to my problem.
What was the central target I wanted to reach?
I wanted the user to gain access to a file stored on a fileserver.
Do I have to provide every displayed entity with the total filepath?
No! Think about the principle of MVC! User actions get processed by the controller just in time. You don't have to provide information untill it really get's used.
So the solution is just to render all data as usual but instead of displaying a static html link to the files, you have to include an ActionLink to the Controller which calculates the filepath on the fly and automatically redirects the user to the file.
In the View do this:
#Html.ActionLink(Model.ID.ToString(), "ShowProductApprovalDocumentFile", "ProductApprovalDocument", new { ProductApprovalDocumentID = Model.ID }, null)
instead of this:
#Model.ID
And add an corresponding Action to the controller:
public ActionResult ShowProductApprovalDocumentFile(int ProductApprovalDocumentID )
{
return Redirect(_unitOfWork.FileService.GetFilePathForProductApprovalDocument(ProductApprovalDocumentID));
}
Thanks to the guys that took the time to give me an answer and special thanks to Andre who lead me to the satisfying answer! :)
If I understand the property correctly, there are several options:
1) Make the FilePath property use a service locator to find the FileService:
public string FilePath {
get {
FileService fileService = DependencyResolver.Current.GetService<FileService>();
return fileService.GetFilePathForDocument(this);
}
}
While I'm not a hugh fan of static service locators as they make testing more difficult, this could be a viable option. To make it more easily testable you can make the file service locator injectable:
private static readonly Func<FileService> defaultFileServiceLocator = ()=>DependencyResolver.Current.GetService<FileService>():
private Func<FileService> fileServiceLocator = defaultFileServiceLocator;
public Func<FileService> FileServiceLocator {
get { return fileServiceLocator; }
set { fileServiceLocator = value ?? defaultFileServiceLocator; }
}
And then use this in FilePath
public string FilePath {
get {
FileService fileService = fileServiceLocator();
return fileService.GetFilePathForDocument(this);
}
}
This way you can inject your own file service locator during testing.
2) Explicitly require the FileService when retrieving the file path. Instead of a FilePath property you'd have:
public string GetFilePath(FileService service){
service.GetFilePathForDocument(this);
}
The problem with this is of course that now the caller of GetFilePath needs to have a FileService. This isn't much of a problem for controllers, because if you use an IoC you can inject a FileService into the controller constructor. This approach is the cleaner one as it doesn't depend on service locators, but as you see it is slightly more inconvenient for the caller.
3) Inject the FileService into the document class itself.
Instead of using a file service locator you'd inject the file service itself when you construct your ProductApprovalDocument. With this approach you can use a simple FilePath property again. The main problem is that this often doesn't play too well with ORMs, as they often construct the objects using a default constructor and you'd have to somehow hook into the object construction process to inject the dependencies. Also, I'm not a big fan of injection services into domain objects.
4) You set the FilePath from outside the entity. As you said this should be done somewhat automatically as you don't want to do it manually every time. This would require some layer through which all entities need to pass which sets up the FilePath property.
5) Don't make FilePath a property of ProductApprovalDocument at all. This would be a reasonable choice, too. ProductApprovalDocument doesn't know anything about its FilePath, so why should it be a property? Its the FileService that calculates the value. You can still have a distinct view model version of ProductApprovalDocument which does have a FilePath property. You'd set the property when you create your view model:
var model = new ProductApprovalDocumentViewModel();
mapper.Map(realDocument, model); // map common properties with AutoMapper or so
model.FilePath = fileService.GetFilePathForDocument(realDocument);
However, if ProductApprovalDocument needs to do something with its FilePath (why would it?) this approach doesn't work anymore.
Personally I'd go with solution 5, 2 or 1 in that order of precedence, where applicable.
Whilst I would be hesitant to rely on being able to calculate the filepath and I would prefer to store it as part of the entity (in case it ever needs to change for some reason), in your situation if I was adamant I wanted to do it the way you've said, I think I would extend the FileService/ViewModel to have a Filepath property which was derived in the fashion you have stated.
e.g. if I wanted to create a download link I'd do this in the ViewModel
public string FilePath
{
get
{
return String.Format(#"thehardcodedbit{0}.pdf",ID);
}
}
EDIT: If you have an Entity generated by EF4.x then it will have been generated as a partial class so you could always extend it like this (I have done this sort of thing and it works okay):
Say the generated entity looks like this:
Namespace Da_Wolf.Model.Entities.File
{
public partial class UploadedFile
{....}
}
Then you could create a partial class like this:
Namespace Da_Wolf.Model.Entities.File
{
public partial class UploadedFile
{
public string FilePath
{
get
{
return String.Format(#"thehardcodedbit{0}.pdf",ID);
}
}
}
}
Now you have the property you desire available everywhere without adding anything to the ViewModels.

Does ASP.NET MVC Has Anything Equivalent To WPF's DataTemplate Feature?

In my ASP.NET MVC project, I have a polymorphic collection that I wish to render - say, an IEnumerable<ISomething> where the individual items may be a mix of different implementations of ISomething.
I'd like that list rendered, where each concrete type renders according to its own template (perhaps a strongly typed ViewUserControl).
In WPF, I'd be able to specify DataTemplates that would automatically bind concrete types to specific templates. Can I do something similar in ASP.NET MVC?
Obviously, I can iterate through the list and attempt a cast using the is keyword and then use a lot of if statements to render the desired control, but I was hoping for something more elegant (like WPF).
I ended up with developing a solution myself - I have described it in DataTemplating In ASP.NET MVC.
I'm not sure if I get you fully, but, why don't you implement a method to your ISomething interface like render for example, which by contract will be implemented to all of your other concrete entities, and then iterate through each item in the polymorphic collection and call it.
I had a similar issue and never found a "simple" answer. I had the benefit of knowing that all items in the list would render the same way so I created a decorator for ISomething, converted the list to IEnumerable using some code from the Umbrella project (http://umbrella.codeplex.com), and then extracted out the relevant pieces. Kinda like the following:
public interface ISomethingDecorator
{
string Description { get; }
string[] Actions { get; }
}
public class BigSomethingDecorator : ISomethingDecorator { /* ... */ }
public class SmallSomethingDecorator : ISomethingDecorator { /* ... */ }
Then, as I said, I use the Umbrella project to convert from ISomething to ISomethingDecorator and returned IEnumerable to the View.
Don't know if it'll help you with what you're trying to do -- especially being a month late -- but I thought I'd let you know how I handled it. If you're displaying completely different formats, it probably won't do the trick but maybe it can get you a starting point.

MVC user created values -- Is reflection my only hope?

Is it possible to determine if a controller exists before attempting to return the View? In my scenerio, my action value in my URL represents a user created value, and the id represents the controller. i.e.
http://mysite.com/systems/WIN1234/Configure
...where WIN1234 is dynamically routed to the Configure action. Because I would like to keep my URLs completely hackable, I would like to determine if Configure exists before...
return View(action)
...where action is my passed in string containing Configure.
The first thing that pops into my head is looking at the assembly using reflection, but before I go that far, and because of my wet ears in MVC, I would like to know if there is a more elegant way to make this determination. i.e. ...something like:
if(DoesControllerExist(action)) return View(action)
...where DoesControllerExist is a built in MVC function.
Any of you experts have any ideas?
Thanks,
George
I'm not sure I get you completely correct, but is this your situation:
http://mysite.com/systems/WIN1234/Configure
Should go to
public class SystemsController : Controller
{
public ActionResult Configure(string theValue) //<== This would be "WIN123" in your example
{
return View(theValue);
}
}
Where you want to make sure that the following view exists:
YourMVCProject
|
+- Views
|
+- Systems
|
+- theValue.aspx
In that case you can use:
private bool ViewExists(string name)
{
return ViewEngines.Engines.FindView(ControllerContext, name, null).View != null;
}
and alter your action to:
public ActionResult Configure(string theValue) //<== This would be "WIN123" in your example
{
if(ViewExists(theValue))
{
return View(theValue);
}
return View(fallBackView);
}
Disclaimer: all freehand code
I think you may be confused as to how the routing system works.
A URL is mapped to a controller and action. If there's no matching controller and action, you'll get an HTTP 404.
Thankfully, URL routes (and of course controllers) are testable so you may want to simply write unit tests for your routing configuration.
On another note, you probably do not want multiple URLs mapping to the same content without issuing an HTTP 301 (permanently moved) since it works against your PageRank.

Resources