Spring MVC rendering 404 due to wrong view - java

I'm debugging a controller method in Spring MVC that should return a view, but I'm getting a 404 page.
Error
HTTP Status 404
/WEB-INF/views/responsive/pages/company/myCompanyCreatePurchaseOrderPage.jsp
The view is actually located in
/WEB-INF/views/pages/company/myCompanyCreatePurchaseOrderPage.jsp
Code
#RequestMapping(method = RequestMethod.GET, value = "/admin/getPoNumbers", produces = "application/json")
protected String getPurchaseOrdersFromSettings(#RequestParam(value = "uid")
final String uid, final Model model, final HttpServletRequest request, final RedirectAttributes redirectAttributes) {
// unrelated code omitted
return "pages/company/myCompanyCreatePurchaseOrderPage";
}
Where does this prefix get set?

InternalResourceViewResolver bean on your web.xml or applicationContext.xml
Sample
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

Related

getting 404 error message while parsing request from 1 jsp file to other

I am using spring MVC, while parsing request from 1 JSP file to other JSP file getting 404 error message
"The requested resource is not available".
#Controller
public class WelcomeController {
#RequestMapping("/login.jsp")
public ModelAndView helloWord() {
ModelAndView model = new ModelAndView("login");
model.addObject("msg", "AYM Registration Form");
return model;
}
#RequestMapping(value="/success.jsp",method=RequestMethod.POST)
public ModelAndView successMessage(#RequestParam Map<String,String> reqPar) {
String name = reqPar.get("name");
String age = reqPar.get("age");
String gender = reqPar.get("gender");
ModelAndView model1 = new ModelAndView("success");
model1.addObject("msg", "You have successfully submitted your data..Pls find: Name="+name+"Age="+age+"gender="+gender);
return model1;
}
}
login.jsp:
<form action="success.jsp" method="POST">
spring-dispatcher-servlet.xml:
<context:component-scan base-package="com.ClassesManagement.Controllers" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
Error Message:
HTTP Status 404 - /FirstMVCProject/success.jsp
type Status report
message /FirstMVCProject/success.jsp
description The requested resource is not available.

No mapping found for HTTP request with URI while redirect

I have registered following viewResolvewer:
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
and wrote following controller method:
#ExceptionHandler(value = Exception.class)
public String redirectToErrorPage(){
return "redirect:/errorPage";
}
When following method executes I see following log:
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/errorPage] in DispatcherServlet with name 'appServlet'
page with name errorPage.jsp locates under the page folder:
Please explain what do I wrong?
You are redirecting to the the URL "/errorPage", but there is no request mapping for /errorPage. You can add a controller with that request mapping e.g.:
#Controller
public class ErrorPageController {
#RequestMapping("/errorPage")
#ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR)
public String showErrorPage(){
return "errorPage";
}
}
Or instead of redirecting, you can just show the error page from your exception handler.
#ExceptionHandler(value = Exception.class)
#ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR)
public String handleException(Exception e){
return "errorPage";
}

How to make Spring SimpleFormController work with HTTP Get requests?

I have Spring SimpleFormController which currently works for POST requests.
I want to change the form submission to GET. So I changed the html form method="post" to method="get".
After the change, I want processFormSubmission method to be invoked.
However its not.
Can you please tell what I am doing wrong here?
import org.springframework.web.servlet.mvc.SimpleFormController;
public class VehicleDescController extends SimpleFormController
{
protected ModelAndView processFormSubmission(
final HttpServletRequest request, final HttpServletResponse response,
final Object command, final BindException errors) throws Exception
{
....
}
}
<bean name="/vehicleDesc.html"
class="com.xxx.VehicleDescController">
<property name="commandName" value="lotSeller"/>
<property name="commandClass" value="com.xxx.LotSeller"/>
<property name="formView" value="xxxTheBasics"/>
<property name="viewName" value="xxxVehicleDesc"/>
<property name="imageUploadViewName" value="imageUpload"/>
<property name="vixErrorView" value="xxxVIXError"/>
<property name="assignmentEntryService" ref="xxxService"/>
<property name="referenceDataService" ref="referenceDataService"/>
<property name="xxxReferenceDataService" ref="xxxReferenceDataService"/>
<property name="messageSource" ref="messageSource"/>
<property name="xxxService" ref="xxxService"/>
<property name="validator" ref="xxxEntryValidator"/>
</bean>
A simple:
#Override
protected boolean isFormSubmission(HttpServletRequest request) {
return true;
}
Will tell it to always follow the submit workflow of the controller. Obviously if there are both "Submits" and also "normal" GETs that come into this URL, you will have to examine the request and figure out the difference!

Unable to redirect to a jsp page using Model and view in Spring

Hi i want to redirect to a jsp page using Model and view but not able to get that page.Below is the code can any one help me on this?
public ModelAndView login(HttpServletRequest request,
HttpServletResponse response) throws GridException {
ModelAndView mv = new ModelAndView("json view");
Map map = new HashMap();
GridMappingVO gridMappingVO=new GridMappingVO();
String users = request.getParameter("username");
String passwrd = request.getParameter("password");
gridMappingVO.setUser(users);
gridMappingVO.setPass(passwrd);
List<GridMappingVO> searchList= gridMappingFacade.validate(gridMappingVO);
if(users.equals(searchList.get(0).getUser()) && passwrd.equals(searchList.get(0).getPass())){
System.out.println("validation succesfull");
mv = new ModelAndView("searchResults");
mv.addObject("searchList", searchList);
}
else{
List<String> list = new ArrayList<String>();
list.add("Invalid");
map.put("error_messages", list);
mv.addAllObjects(map);
}
return mv;
}
}
*This the spring config which am using
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<import resource="datasource-config.xml"/>
<import resource="dao-config.xml"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="xmlFileViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="order" value="2" />
<property name="location" value="/WEB-INF/views.xml" />
</bean>
<bean name="/*.html" class="com.dashboard.web.controller.GridMappingController" scope="request">
<property name="gridMappingFacade" ref="gridMappingFacade"/>
</bean>
<bean id="gridMappingFacade" class="com.dashboard.web.application.GridMappingFacadeImpl">
<property name="gridMappingService" ref="gridMappingService" />
</bean>
<bean id="gridMappingService" class="com.dashboard.web.service.GridMappingServiceImpl">
<property name="gridMappingRepository" ref="gridMappingRepository" />
</bean>
</beans>*
use this code to redirect page
mv.setViewName("searchResults");

Change Name of a .JSP file

I am developing a simple spring application. I have a few jsps and I would like to change the name and the URL of a jsp. I changed the controller:
#RequestMapping(value = "/simpleForm.html", method = RequestMethod.GET)
public void simpleForm(Model model) {
model.addAttribute(new User());
}
to
#RequestMapping(value = "/newName.html", method = RequestMethod.GET)
public void simpleForm(Model model) {
model.addAttribute(new User());
}
and the name of the old simpleForm.jsp to newName.jsp user is a class I use in the form in simpleform.jsp
I couldn't make it work. I am getting 404 that simpleform.jsp is not found. I am pretty stuck.
Edit: My view resolver tags:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
My url pattern is like:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/forms/*</url-pattern>
</servlet-mapping>
I've found out that all of the links are getting the same error ()
resource not available. Even the ones that I didn't change the name
of.
I also tried directly starting from newName.jsp. Still the same error!
change:
#RequestMapping(value = "/newName.html", method = RequestMethod.GET)
to:
#RequestMapping(value = "/newName.jsp", method = RequestMethod.GET)
Double-check you still have the #Controller annotation on the class. I've seen 404s when has been accidentally deleted.
Try changing it to
#RequestMapping(value = "/newName.html", method = RequestMethod.GET)
public String simpleForm(Model model) {
model.addAttribute(new User());
return "newName"; // returning the desired view
}
And make sure you have defined viewResolver accordingly like
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
Change prefix as per your application structure.It will take the view (String returned by controller) and will add prefix and suffix. So the view resolved will be /WEB-INF/jsp/newName.jsp.
A 404 error is an HTTP status code that means that the page you were trying to reach on a website couldn't be found on their server.
As I can see, your suffix is .jsp. So try this (if there is any newName.jsp in the WebContent)
#RequestMapping(value = "/newName", method = RequestMethod.GET)
public void simpleForm(Model model) {
model.addAttribute(new User());
}
It should work
If you try this URL
http://localhost:<port>/AppNAme/forms/newName.html
and your controller is
#RequestMapping(value = "/newName.html", method = RequestMethod.GET)
public ModelAndView simpleForm(Model model) {
model.addAttribute(new User());
ModelAndView mv = new ModelAndView("jspViewName");
return mv;
}
The forward slash is missing in the prefix of your ViewResolver. This should fix your 404. Give it a shot.
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>

Categories

Resources