A. Create a new MVC application. B. Add a controller to it so that “/Product” wi
ID: 3879369 • Letter: A
Question
A. Create a new MVC application.
B. Add a controller to it so that “/Product” will work.
C. Change the Index ActionMethod to return a string “Product/Index is displayed”.
D. Add ActionMethods to the appropriate controller to make the following URLs work.
i. /Products/Browse
This should display a string “Browse displayed”
ii./Products/Details/105
This should display a string “Details displayed for Id=105”
iii./Products/Location?zip=44124
This should display a string “Location displayed for zip=44124”
E. Add breakpoint to each method and take screenshots
F. Access the above URLS from your localhost and capture the results as screenshots
G. Make sure that you Commit and Push the project (as needed).
Explanation / Answer
package com.javatpoint;
import javax.persistence.QueryHint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class ProductsController {
@RequestMapping("Products/Index")
public ModelAndView index(){
String message="Products/Index Displayed";
return new ModelAndView("products","message",message);
}
@RequestMapping("Products/Browse")
public ModelAndView Browse(){
String message="Browse Displayed";
return new ModelAndView("products","message",message);
}
@RequestMapping("Products/Details")
public ModelAndView Details(@RequestParam String Id){
String message="Details Displayed for Id="+Id;
return new ModelAndView("products","message",message);
}
@RequestMapping("Products/Location")
public ModelAndView Location(@RequestParam("zip") String zip){
String message="Location displayed for zip="+zip;
return new ModelAndView("products","message",message);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*</url-pattern>
</servlet-mapping>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/jsp/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.javatpoint" />
</beans:beans>
products.jsp
Message: ${message}
index.jsp
Product Index :
<a href="Products/Index">Click Here</a>
<br></br>
Product Browse :
<a href="Products/Browse.html">Click Here</a>
<br></br>
Product Details :
<a href="Products/Details.html?Id=105">Click Here</a>
<br></br>
Product Location :
<a href="Products/Location.html?zip=44124">Click Here</a>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.