spring mvc forward to jsp
Posted
by
jerluc
on Stack Overflow
See other posts from Stack Overflow
or by jerluc
Published on 2011-02-17T17:46:07Z
Indexed on
2012/11/06
5:01 UTC
Read the original article
Hit count: 229
I currently have my web.xml configured to catch 404s and send them to my spring controller which will perform a search given the original URL request.
The functionality is all there as far as the catch and search go, however the trouble begins to arise when I try to return a view.
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" p:order="1">
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="jsp" value="text/html" />
</map>
</property>
<property name="defaultContentType" value="application/json" />
<property name="favorPathExtension" value="true" />
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value="" />
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</list>
</property>
<property name="ignoreAcceptHeader" value="true" />
</bean>
This is a snippet from my MVC config file.
The problem lies in resolving the view's path to the /WEB-INF/jsp/
directory. Using a logger in my JBoss setup, I can see that when I test this search controller by going to a non-existent page, the following occurs:
Server can't find the request
Request is sent to 404 error page (in this case my search controller)
Search controller performs search
Search controller returns view name (for this illustration, we'll assume
test.jsp
is returned)Based off of server logger, I can see that
org.springframework.web.servlet.view.JstlView
is initialized once my search controller returns the view name (so I can assume it is being picked up correctly by theInternalResourceViewResolver
)Server attempts to return content to browser resulting in a 404!
A couple things confuse me about this:
I'm not 100% sure why this isn't resolving when
test.jsp
clearly exists under the/WEB-INF/jsp/
directory.Even if there was some other problem, why would this result in a 404? Shouldn't a 404 error page that results in another 404 theoretically create an infinite loop?
Thanks for any help or pointers!
Controller class [incomplete]:
@Controller
public class SiteMapController {
//--------------------------------------------------------------------------------------
@Autowired(required=true)
private SearchService search;
@Autowired(required=true)
private CatalogService catalog;
//--------------------------------------------------------------------------------------
@RequestMapping(value = "/sitemap", method = RequestMethod.GET)
public String sitemap (HttpServletRequest request, HttpServletResponse response) {
String forwardPath = "";
try {
long startTime = System.nanoTime() / 1000000;
String pathQuery = (String) request.getAttribute("javax.servlet.error.request_uri");
Scanner pathScanner = new Scanner(pathQuery).useDelimiter("\\/");
String context = pathScanner.next();
List<ProductLightDTO> results = new ArrayList<ProductLightDTO>();
StringBuilder query = new StringBuilder();
String currentValue;
while (pathScanner.hasNext()) {
currentValue = pathScanner.next().toLowerCase();
System.out.println(currentValue);
if (query.length() > 0)
query.append(" AND ");
if (currentValue.contains("-")) {
query.append("\"");
query.append(currentValue.replace("-", " "));
query.append("\"");
}
else {
query.append(currentValue + "*");
}
}
//results.addAll(this.doSearch(query.toString()));
System.out.println("Request: " + pathQuery);
System.out.println("Built Query:" + query.toString());
//System.out.println("Result size: " + results.size());
long totalTime = (System.nanoTime() / 1000000) - startTime;
System.out.println("Total TTP: " + totalTime + "ms");
if (results == null || results.size() == 0) {
forwardPath = "home.jsp";
}
else if (results.size() == 1) {
forwardPath = "product.jsp";
}
else {
forwardPath = "category.jsp";
}
}
catch (Exception ex) {
System.err.println(ex);
}
System.out.println("Returning view: " + forwardPath);
return forwardPath;
}
}
© Stack Overflow or respective owner