I have an Spring MVC 6 app deployed in Tomcat 10.1. the context root of the app is /codereaper as that is the name of the war file. The app starts successfully with no errors.
I am using a UrlBasedViewResolver with the following properties.
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/pages/" /> <property name="suffix" value=".jsp" /> </bean>
and the home.jsp in the /WEB-INF/paqes/ folder in the war file If I want my MainController to be as follows to render the home page for /codereaper/home what should the MainController annotation mapping be.
Here is what I am trying in my MainController. So the context root for my app is /codereaper and I THINK that the Main controller annotation of / would equate to /codereaper/home since the / is relative and postpended to the Context root of the app.
I asked the Tomcat folks and they thought that this was correct as the / is relative to the Container context root.
In a previous post, I was flamed for suggesting this and was told that I needed to add /codereaper to all of my Controllers to achieve this effect.
@Controller @RequestMapping("/")
public class MainController {
static Logger logger = LogManager.getLogger(MainController.class);
@RequestMapping("/home")
public ModelAndView home(Model model, HttpServletRequest request, HttpServletResponse response)
{
ModelAndView mav = new ModelAndView();
MainContent mainContent = new MainContent();
String username;
model.addAttribute("userName", "Username");
mav.setViewName("home.jsp");
return (mav);
}
I have tried adding /codereaper to the MainController annotation and also just a /.
I always get a 404 and in my tomcat logs I see:
192.168.0.28 – – [10/Sep/2024:18:07:03 -0400] “GET /codereaper/home HTTP/1.1” 404 7651
And in app logs I see
DispatcherServlet [http-nio-80-exec-7] GET “/codereaper/home”, parameters={}, headers={masked} in DispatcherServlet ‘codereaperServlet’
h.SimpleUrlHandlerMapping [http-nio-80-exec-7] Mapped to HandlerExecutionChain with [org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler@2b3472a0] and 2 interceptors
DispatcherServlet [http-nio-80-exec-7] No view rendering, null ModelAndView returned.
DispatcherServlet [http-nio-80-exec-7] Completed 404 NOT_FOUND, headers={}
The app redirects to my 404 error page and I put the following in the JSP
<h1 class="text-dark fw-bolder" align="center">Page Not Found Error - ${pageContext.request.contextPath} </h1>
and it renders Page Not Found Error – /codereaper so I know the context root is correct.
BTW this paradigm works in a Spring MVC 4 app with Tomcat 9.0