I am trying to create a Spring MVC application. While starting the application in server itself I need some to implement some excel file reading logic. I am new to Spring. Can I use interceptors for this? Once the excel is read it will be saved in a map and that map will be used for the rest of the things.
Use a PostConstruct method to achieve the desired requirement. This will be called when all the dependencies are injected and when container is brought up.
#PostConstruct
public void loadData(){
// Read from your excel file here.
}
Related
I'm just learning asp.net mvc and I'm trying to figure out how to move my controllers into a separate project. Typically when I have designed asp.net web apps before, I created one project for my models, another for my logic, and then there was the web.
Now that I'm learning asp.net mvc I was hoping to follow a similar pattern and put the models and controllers each into their own separate projects, and just leave the views/scripts/css in the web. The models part was easy, but what I don't understand is how to make my controllers in a separate project be "found". Also, I would like to know if this is advisable. Thanks!
First of all, it is certainly a good idea to put your model into a separate project. As you've discovered, this is trivial.
Regarding Controllers and Views, I don't see any obvious advantage to separating them for most basic projects, although you may have a particular need to do so in a particular application.
If you do choose to do this, then you will need to tell the framework how to find your controllers. The basic way to do this is by supplying your own ControllerFactory. You can take a look at the source code for the DefaultControllerFactory to get an idea for how this is done. Subtyping this class and overriding the GetControllerType(string controllerName) method may be enough to accomplish what you're asking.
Once you've created your own custom ControllerFactory, you add the following line to Application_Start in global.asax to tell the framework where to find it:
ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());
Update: Read this post and the posts it links to for more info. See also Phil Haack's comment on that post about:
ControllerBuilder.Current.DefaultNamespaces.Add(
"ExternalAssembly.Controllers");
...which is not a complete solution, but possibly good enough for simple cases.
While it is reasonable to create your own ControllerFactory, I found it more convenient to define all my Controllers in each project, but derive them from Controllers in my Shared project:
namespace MyProject1.Controllers
{
public class MyController : MySharedProject.Controllers.MyController
{
// nothing much to do here...
}
}
namespace MySharedProject.Controllers
{
public abstract class MyController : System.Web.Mvc.Controller
{
// all (or most) of my controller logic here...
}
}
This has the added benefit that you have a place to put your Controller logic that differs from project to project. Also, it is easier for other developers to quickly find your Controller logic because the Controllers exist in the standard place.
Regarding whether this is advisable, I think it absolutely is. I've created some common Account Management logic that I want to share between projects that otherwise have very different business logic. So I'm sharing my Account and Admin Controllers, but the other Controllers are specific to their respective projects.
Add the Class Library for your mvc project.
In the class add the following code(For u'r Controller Code)
namespace ContactController
{
public class ContactController : Controller
{
public ActionResult Call()
{
ViewBag.Title = "Inside MyFirst Controller.";
return View();
}
}
}
On the mvc project view folder add the folder for Contact and create a Call.cshtml file.
Add the class library project reference into your main MVC project.
Finally to refer contact controller namespace into Route Config.
My problem solved after I updated System.Web.Mvc NuGet reference so MvcWebsite and Class Library use same System.Web.Mvc version
No need to add default namespaces
The simplest form of separation I use is to retain the Views "as is" in the original MVC project but remove the Controllers. Then in a new ClassLibrary project add the Controller classes and ensure they inherit from Controller.
The MVC routing engine will automatically route to the Controllers in the ClassLibrary and the Controllers will automatically construct the Views from the original MVC project, provided you have your references and usings correctly in place.
I am using this architecture to implement an Html Reports module that can be compiled and deployed separately from the main solution. At last I am free from SSRS!
In previous version of asp.net the HostingEnvironment had MapPath method to get and store the path of the file but in ASP.net 5 I can't use it.
var filepath = HostingEnvironment.MapPath(#"~/Data/product.json");
Are you using RC1?
In RC1 it depends if you are writing a console or web app. In console apps, you cannot use DI anymore but PlatformServices.Default.
PlatformServices.Default.Application gives you access to the base path of your application for example. The type of the static property is IRuntimeEnvironment. And having the base path of your app, you can easily build the path to files you need...
If you are building a web app, you should be able to inject IRuntimeEnvironment to your startup and use it from there.
You have to add a reference to the Microsoft.Extensions.PlatformAbstractions package to all that stuff.
See also my post here for more details
Despite IHostingEnvironment provides MapPath() method you may also need UnmapPath() too. Another useful method might be IsPathMapped().
You can find all of them here: Reading a file in MVC 6.
And all of them, thanks to PlatformServices availability, work in Consolse, MVC, and ClassLib apps.
HTH
I need to store "hostName" parameter in my Spring MVC application configuration (for writing links in templates to static recourses, which are on static.hostName). I assume hardcoding this is bad, so where should I store it?
Web.xml, or servlet-context.xml? And how do I get it?
Thanks.
With a PropertyPlaceholderConfigurer you can externalize beans (and primitives) into a properties file. You can inject those beans with SpEl and #Value:
#Value("${hostname}")
private String hostname;
You can find a configuration example in the reference documentation.
I just started to get into ASP.net Web API (created MVC 4 project, Web API application, in .net 4.5).
I need to make custom handlers in particular. All I know is that we create one (inherit DelegatingHandler), and register it in App_Start/WebApiConfig.cs's Register function.
Where do we save such a handler (MyMessageHandler), though? No tutorial or book I've come tells me this. I tried to save it in the App_Start folder with the same namespace as WebApiConfig, but it says that MyMessageHandler cannot be found:
config.MessageHandlers.Add(new MyMessageHandler()); // MyMessageHandler is not found.
Where you store MyMessageHandler.cs doesn't matter at all, as long as you get your references in WebApiConfig right. I personally store them like project_root/MessageHandlers/MyMessageHandler.cs
I've been using the Ninject.Web extension to inject business objects, repositories, Entity Framework context etc into my application. This works very well using the [Inject] attribute which can be applied within a webform that inherits from PageBase. I am now running into a snag as I am trying to write a custom membership provider that needs injection done inside of it but of course this provider is not instantiated from within a webform. Forms Authentication will instantiate the object when it needs it. I am unsure how to about doing this without having access to the [Inject] attribute. I understand that there is an application level kernel somewhere, but I have no idea how to tap into it. Any suggestions would be greatly appreciated.
You don't have to use the service locator pattern, just inject into properties of your custom membership provider in Application_Start. Assuming you've registed the providers properly you can do this with something like:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
// Inject account repository into our custom membership & role providers.
_kernel.Inject(Membership.Provider);
// Register the Object Id binder.
ModelBinders.Binders.Add(typeof(ObjectId), new ObjectIdModelBinder());
}
I've written up a more in depth explanation here:
http://www.danharman.net/2011/06/23/asp-net-mvc-3-custom-membership-provider-with-repository-injection/
You do a IKernel.Inject on the the instance. Have a look at the source for the Application class in the extension project you're using.
In the case of V2, it's in a KernelContainer. So you need to do a:
KernelContainer.Inject( this )
where this is the non-page, non application class of which you speak.
You'll need to make sure this only happens once - be careful doing this in Global, which may get instantiated multiple times.
Also, your Application / Global class needs to derive from NinjectHttpAppplication, but I'm sure you've that covered.
you may need to use the Service Locator pattern, since you have no control over the creation of the membership provider.