Wednesday, January 26, 2022

How to make spring controller Get request on controller









Let's begin dilsecodie  

Spring Web model-view-controller (MVC) framework

Get code example from - dilsecodie

Step 1: GOTO spring MVC section, find spring controller 
Step 2:  use code in your class, which you want to make the class a controller.
Step 3: In @RequestMapping value means request URL endpoint, changes its values as per your requirement.
Step4: before step 3 check, the class uses @controller and spring dependency.
Step5: @ModelAttribute is used for getting request parameters from URL.
Step6: Make service in the controller according to business logic, then return model view.
Step7: Modelview means  view you need to show model output in HTML page (or JSP page)
Step8: Controller can also call another controller can see in example return showData(); 
, as showData(); method calls another controller then model view return.


let's understand the working of the spring controller,
First request come to dispatcher servlet in web.xml file 
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
then spring created the objects which are mentioned in xml file 
<context:component-scan base-package= Mention your folder which containes 
all the files .
<context:exclude-filter expression="org.springframework.stereotype.Controller"
type="annotation" />
</context:component-scan> 
use this tag will scan your controller which provide correct controller 
for request end point 

These property need to define in applicationContext.xml file 

<mvc:annotation-driven />  also its tag is one of important tag bcoz 
its autowired your all beans 

<property name="packagesToScan" value="com.your.packages"></property> 
its also used for all files scan so spring container can provide you object of bean

So when spring provide request a correct controller Then 
Controller class should have @Controller define in it .
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

these packages of spring should be import in your class .

For get request handle using GET method 
@RequestMapping(value={"getDilsecodieUserData"}, 
method={org.springframework.web.bind.annotation.RequestMethod.GET})
public ModelAndView getDilsecodieGetData
(HttpServletRequest request,HttpSession session){
ModelAndView mv = new ModelAndView("dilsecodie/index");
Write your service to implement business logic , and add model object 
return mv;
 }

For post request handler using post method 

@RequestMapping(value={"askquestionsdilsecodie"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
ModelAndView askquestionDilsecodie(HttpServletRequest req, HttpSession session) 
throws CustomException{

String value1= req.getParameter("value1");

String value2= req.getParameter("value2");

String value3= req.getParameter("value3");
ModelAndView mv1 = new ModelAndView("dilsecodie/dashboard");
Write your service to implement business logic , and add model object 
return mv1;
 }
 @RequestMapping("sendStringvalue")
@ResponseBody
String saveShortlisting(HttpServletRequest req, HttpServletResponse res){
msg = "success";
return msg;
}
@RequestMapping("sendStringvalue")
@ResponseBody
String saveStories(HttpServletRequest req, HttpServletResponse res){
msg = "success";
return msg;
}

If needed to handle any type of request both post or get request then use 

@RequestMapping("updateInterviewStatus")
ModelAndView updateStatus(HttpServletRequest req, HttpServletResponse res){
ModelAndView mv3 = new ModelAndView("dilsecodie/das");
Write your service to implement business logic , and add model object 
return mv3;
}





No comments:

Post a Comment