What java directory structure do you want? - model-view-controller

While working, I suddenly thought of it.
Each company has a slightly different directory structure.
controller, domain, service(in serviceimpl), repository
controller, dto, dao, service(in serviceimpl diretory)
controller, domain(in dto diretory), service(in serviceimpl diretory) ,repository(in dao dir diretory)
controller, domain, service, dao
controller, service(in dao, service, vo file and serviceimpl directory) etc..
The names are different, and I have a headache due to various configurations
What is the directory structure pursued?
Are there any other companies that have a structure other than this?
I need the exact name and directory structure that Java pursues.
But this kind of idealistic idea has never always come true..
What do you think is the ideal structure?

Related

Struts 2 tomcat request/session contamination

I am using Struts 2 v 2.3.16.3 with tomcat 6.
A user will click on an action which finds an object by id and the page displays it. I have encountered a sporadic bug where the user will all of a sudden get the id of another lookup from another user on another machine. So effectively they are both calling the same action but passing different id to the request, but both end up viewing the same id.
This is obviously disastrous, and the data is totally corrupted as both users think they are editing a different record. Any ideas how make sure session/request activity is kept secure to each session?
I am also using spring and am using the #Transactional annotation in my Service layer, which returns the objects from the DAO. Is there something I need to do with this annotation to make it secure for each session ?
I am using org.springframework.orm.hibernate3.HibernateTransactionManager
Classic Thread-UnSafe problem.
Since you nominated Spring, my first guess is that you have not specified the right scope for your action beans in Spring xml configuration.
Be sure you are using scope="prototype" because otherwise the default scope of Spring is Singleton, and you don't want a single(ton) instance of an Action, that would not be ThreadLocal (and hence ThreadSafe) anymore.
If it is not that, it could be something on an Interceptor (that, differently from an action, is not Thread Safe), or you are using something static (in your Business / DAO layer, or in the Action itself) that should be not.

Spring environment validation

We're building a Spring-based application which will be delivered to end users as a distribution package. Users are responsible for properly configuring whatever needs to be configured (it's mostly about various filesystem locations, folder access permissions, etc). There's a good idea to make the app help users understand what is not configured or which parts of configuration are invalid.
Our current approach is a custom ApplicationContextInitializer which does all the environment validation "manually" and then registers few "low level" beans in the application context explicitly. If something is wrong, initializer throws, exception is caught somewhere in main(), interpreted (converted into plain English) and then displayed.
While this approach works fine, I'm wondering if there are any best practices to minimize hand-written code and use Spring whenever possible.
Here's an illustrative example. The application requires a folder for file uploads. This means:
There should be a configuration file
This file should be accessible by the app
This file should have no syntax errors
This file should explicitly define some specific property (let it be app.uploads.folder)
This property should describe the existing filesystem entity
This entity should be a folder
The app should have read/write access to this folder
Does Spring provide any tools to implement this sort of validation easily?
Spring Boot has a nice feature for context and external configuration validation. If you define a POJO class and declare it as #ConfigurationProperties then Spring will bind the Environment (external properties and System/OS typically) to its properties using a DataBinder. E.g.
#ConfigurationProperties(name="app.uploads")
public class FileUploadProperties {
private File folder;
// getters and setters ommitted
}
will bind to app.uploads.folder and ensure that it is a File. For extra validation you can do it manually in the setter, or you can implement Validator in your FileUploadProperties or you can use JSR-303 annotations on the fields. By default an external property in app.uploads.* that doesn't bind will throw an exception (e.g. a mis-spelled property name, or a conversion/format error).
If you use Spring Boot Autoconfigure #EnableAutoConfigure you don't have to do anything else, but if it's just vanilla Spring (Boot) you need to say #EnableConfigurationProperties in your #Configuration somewhere as well.
A bonus feature: if you also use the Spring Boot Actuator you will also get JMX and HTTP support (in a webapp) for inspecting the bindable and bound properties of #ConfigurationProperties beans. The HTTP endpoint is "/configprops".

realPath for MockHttpServletRequest

I am writing a Spring based Test case for my Spring Controller, in my controller we are using like this "request.getSession().getServletContext().getRealPath("/myFolderName");"
Its working fine for normal requests, but for unit test cases like MockHttpServletRequest it is the above method call is giving null. how can i get realPath for MockHttpServletRequest.
Thanks,
Praneeth.
By default MockHttpServletRequest acts as if root webapp folder is a classpath, therefore you should be able to use getRealPath() for classpath resources.
Alternatively, you can supply MockHttpServletRequest with MockServletContext, and MockServletContext can be configured with specific root folder for getRealPath(), etc.

Autofac equivalent of Ninject's WhenInjectedInto()

So we're working on converting some projects at work from Ninject to Autofac, and we've stumbled on something really neat within Ninject that we can't figure out how to do in Autofac. In our application, we have an interface called ISession which is implemented in two different concrete types. One goes to an Oracle database, and the other goes to an MS-SQL database.
We have controllers in our MVC application that need only one concrete implementation of ISession based on which controller they are being injected into. For example:
Bind<IFoo>().To<Foo1>();
Bind<IFoo>().To<Foo2>().WhenInjectedInto<OracleController>();
My question is: how do we achieve the same result in Autofac? When IFoo is injected into any controller, Foo1 should be provided by default, however in one special case, we need Foo2 instead.
Thanks for any help in advance!
With Autofac you can achieve this by doing the registration the other way around. So you should declare that you want to use the "speciel" service when you register the OracleController not when you register the IFoo.
containerBuilder.RegisterType<Foo1>().As<IFoo>();
containerBuilder.RegisterType<Foo2>().Named<IFoo>("oracle");
containerBuilder.RegisterType<OracleController>().WithParameter(ResolvedParameter.ForNamed<IFoo>("oracle"));
The Named registration "oracle" ensures that the default IFoo instance will be Foo1 and you only get Foo2 when you explicitly request it by name.

Spring Security + Spring MVC how to integrate them properlly?

I have a working project with both frameworks doing fine
My problem is how to architect it.
I have set 3 Roles ( Admin, Operator, User), and one folder for each of them. like thid
WebContent
|___admin
|___user
|___operator
|___WEB-INF
|__jsp
in admin,user, and operator are those roles only jsps
in jsp are all public jsps.
Is this the right way to do it ?
because when I create my viewResolver it only points to Web-Inf->jsp... So I am having trouble to make my requests land on those folders.
I want to be able to put a .jsp in a folder so it would automatically be assigned to a Role.
But I would like to be able to reuse the JSPs too.
Should I create another view resolver to each of those folders ?
Should they all be inside the WEB-INF folder ?
What is the best (more used and simple) way of doing this ?
I just added the user role folders into WEB-INF and used redirect... now it's working.

Resources