使用Spring MVC 3无法访问JSP页面,附addContact.jsp代码求排查
Hey there, let's troubleshoot why your addContact.jsp isn't accessible in Spring MVC 3. I'll go through the most common fixes step by step:
Spring MVC relies on a view resolver to map controller return values to actual JSP files. First, confirm you have this configured in your dispatcher-servlet.xml (or your Spring context config file):
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <!-- Update this to match your JSP's directory --> <property name="suffix" value=".jsp" /> </bean>
- Make sure your
addContact.jspis placed in the directory specified by theprefixattribute. For example, if the prefix is/WEB-INF/views/, your JSP should live at/WEB-INF/views/addContact.jsp. - Avoid placing JSPs outside the
WEB-INFfolder if possible—this prevents direct access to the JSP bypassing Spring's controller logic, which is more secure.
To display the form, you need a controller method that returns the view name addContact (matching your JSP name without the .jsp suffix). Here's a working example:
@Controller @RequestMapping("/contacts") public class ContactController { // Handles GET request to show the form @RequestMapping(value = "/add", method = RequestMethod.GET) public String showAddContactForm(Model model) { // Add the contact model attribute required by your JSP's <form:form> tag model.addAttribute("contact", new Contact()); return "addContact"; // Maps to addContact.jsp via the view resolver } // Handles POST request to submit the form @RequestMapping(value = "/add", method = RequestMethod.POST) public String processContactSubmission(@ModelAttribute("contact") Contact contact) { // Add your form processing logic here (save to DB, etc.) return "redirect:/contacts/list"; // Redirect to a contact list page after submission } }
- Ensure you're accessing the correct URL: if your app's context root is
myapp, visithttp://localhost:8080/myapp/contacts/add(a GET request) to trigger the form display. - Double-check that the
modelAttribute="contact"in your JSP matches what you're adding to the Model in the controller—Spring will throw an error if this attribute is missing, which can prevent the page from loading.
Your web.xml needs to correctly map the Spring DispatcherServlet to handle incoming requests:
<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>/</url-pattern> <!-- Or use /contacts/* to restrict to contact-related paths --> </servlet-mapping>
- Avoid using
*.jspas theurl-pattern—this will bypass the DispatcherServlet for JSP requests, meaning Spring won't handle the routing to your form page. Stick to/or a specific path like/contacts/*.
- Filenames are case-sensitive on some servers (like Linux), so make sure your JSP is named exactly
addContact.jsp(no uppercase/lowercase mismatches). - Confirm the Spring form taglib declaration in your JSP is correct:
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>—and ensurespring-webmvc.jaris in your project's classpath (missing this JAR will break the form tags). - If you're getting a 404 error, check your server logs—they'll usually tell you exactly which path Spring was trying to resolve for the view, which helps pinpoint the issue.
If your JSP is outside the WEB-INF folder (not recommended for security), try accessing it directly via a URL like http://localhost:8080/myapp/addContact.jsp. If it loads, the problem is likely in your controller or view resolver configuration.
After going through these checks, your addContact.jsp should load properly when you hit the correct GET endpoint. If you run into a specific error message, feel free to share it and I can help narrow things down further!
内容的提问来源于stack exchange,提问作者Anton Prudyus

