You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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:

1. Validate Your View Resolver Setup

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.jsp is placed in the directory specified by the prefix attribute. 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-INF folder if possible—this prevents direct access to the JSP bypassing Spring's controller logic, which is more secure.
2. Check Your Controller Mapping

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, visit http://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.
3. Verify Your web.xml Configuration

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 *.jsp as the url-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/*.
4. Double-Check for Typos & Path Errors
  • 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 ensure spring-webmvc.jar is 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.
5. Test Direct JSP Access (If Applicable)

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:55:03