This is the topic about a stable framework called Spring that allows us to develop software especially by using JAVA language. Indeed, Spring Framework was developed as a software developing kit for working on JAVA and .NET. With the help of its extra components like (Spring Web Flow or Spring Framework itself etc.) web pages can be developed using JAVA Enterprise platform. Among other frameworks, Spring has come a step forward to become an alternative EJB(Enterprise JavaBean) model.
The ones who want to develop a software using Spring Framework must implement the Spring needed components to their projects. Either, while you are creating a new project, you can select Spring from the desired technologies part of the window, or, after creating an empty project, you can make all needed configurations by yourself manually - which I don't really want to cope with -.
Here is the screenshot of the project start up window. I feel myself comfortable with using IntellijIDEA 11.1.3 for IDE and my Spring Framework version is 3.1.1. These version information are 'must be given' data for a blogger. Because I had lots of troubles relevant to version differences of Spring itself, Spring Security and Spring Webflow and whenever I checked any blogs or any other Spring tutorials, they do the same solutions as mine, however mine was not resolved because of the version differences. This is a mandatory check for all the programmers if they don't want to get an headache. Use latest and stable versions of the technologies you prefer to use.
Spring Framework is an easy to use technology once after you learnt it. In order to make learning process less stressful, you should work clean as I mentioned in my other blog topics. 'Working clean' term is based on 4 main components.
1) Controller,
2) Form,
3) Validator,
4) JSP file.
All these components must be collected under seperate main classes in order to find them easily, or, if your project contains too many different features, you can put all these components (not including the JSP file, it should be under another file called 'jsp') under the name of that feature.
Here as you see my related java classes are in one part (above one), and other related jsp files are under the jsp file(below one). I divide my project structure feature by feature, since its a big one, so it's easy to find for me what I'm searching for.
1. Controller
Controller is the key part of the hierarchy which is a bridge between the view (JSP file) and the backend(server-side) of the project. You manage all the operations at the backend side and return the result to controller and preferentially you can put the result to a map, to the HttpSession, or in a model and put the result in a ModelandView object and return it to ModelandView object. After that you can call the related value from JSP file.
@RequestMapping(value = "/pri/SuperUser/ResetPassword.qib",
method = RequestMethod.POST)
public ModelAndView submit(@ModelAttribute("resetPasswordForm") ResetPasswordForm resetPasswordForm,
BindingResult bindingResult,
HttpSession session,
Model model){
Map<String,Object> map = new HashMap<String, Object>();
map.put("response1", "Here can be a object comes from backend side");
session.setAttribute("response2", "Here can be a object comes from backend side");
return new ModelAndView("/pri/SuperUser/ResetPassword", map);
}
This code piece is from my Controller. As I mentioned above I can put my result in a map or in a session to view it later on my JSP. No need to return session because it derives from HttpSession which will always be written to session without any return value.However the map you created must be returned via ModelandView Object.
I want to take your attention to the very begining part of my code. There is a @RequestMapping attribute there. Well, here is the Spring magic! Spring communicate with itself by using these '@' tags. You put these '@'s almost everywhere of your project as long as you want Spring to read the codes you wrote. Here, RequestMapping attribute takes the requests to this URL (within the 'value' tag) and progresses it.
The thing that you shouldn't forget before start to write your Controller class is, you should always write @Controller before your Controller class starts otherwise your Controller class will never be seen and the requests that you made will not be delivered to @RequestMapping attribute;
@Controller
public class ResetPasswordController extends SomeNeededClassesQueue {
...
...
...
}
The final issue about the Controller component is the return value of @RequestMapping related class. You can either return String or Modelandview object. However in both cases, you should certainly be giving the path of your JSP file that you want to proceed/show the info from your requestmapping method.
return "redirect:/pri/SuperUser/ResetPassword";
or
return new ModelAndView("/pri/SuperUser/ResetPassword", map);
If you want to return a series of information, you should put them in a map, which means you should return to ModelandView. Actually you can still return to String, but therefore you should but this map in a session or in a model which we don't want to use these because of the security reasons. You must not put everyting in a session. Model also puts things in a session, but just for that page, but again, it's a security failure.
By the way, the method name for your requestmapping has no importance. Even you can put "aasdfasdf" :) You never call it by its method name.
By the way, the method name for your requestmapping has no importance. Even you can put "aasdfasdf" :) You never call it by its method name.
...to be continued under this topic
Hiç yorum yok:
Yorum Gönder