【问题标题】:Error in navigating one page to another using Spring MVC framework?使用 Spring MVC 框架将一个页面导航到另一个页面时出错?
【发布时间】:2013-11-29 08:08:39
【问题描述】:

我想在我的项目中使用 spring MVC 从一个页面导航到另一个页面。

我有两个 JSP 页面和一个控制器。 第一页是欢迎页面,当我使用 tomcat 启动项目时打开。 我的项目能够显示欢迎页面。但它找不到映射。

欢迎页面代码:

<a href="hello.html">Say Hello</a>

第二页代码:

<body> ${message} </body>

控制器代码:

@Controller
public class HelloWorldController {

    @RequestMapping("/hello")
    public ModelAndView helloWorld() {

        String message = "Hello World, Spring 3.0!";
        System.out.println(message);
        return new ModelAndView("hello", "message", message);
    }

}

Servlet xml 代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
    <context:component-scan
        base-package="net.viralpatel.spring3.controller" />

     <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
           <property name="prefix" value="/WEB-INF/jsp/"/>
           <property name="suffix" value=".jsp"/>

        </bean>
        <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
</beans>

网页xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>Spring3MVC</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
</web-app>

请帮帮我。

【问题讨论】:

  • 精确地定义发生的事情。如果您收到错误消息,请将其完全粘贴。
  • 尝试将 @RequestMapping("/hello") 更改为 @RequestMapping("/hello.html") 并在此之后为您的控制器类添加请求映射,例如 @RequestMapping("/helloWorld") 确保在浏览器中您的最终 URL 应该是像这样localhost:8080/appName/helloWorld/hello.html
  • &lt;servlet-mapping&gt; &lt;servlet-name&gt;spring&lt;/servlet-name&gt; &lt;url-pattern&gt;*.html&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; 在你的配置中你已经给出了你所有的请求应该像 .html
  • user230391 问题解决了吗??

标签: java xml spring jsp spring-mvc


【解决方案1】:
@Controller
@RequestMapping("/helloWorld")
public class HelloWorldController {
@RequestMapping("/hello.html")
    public ModelAndView helloWorld() {
    String message = "Hello World, Spring 3.0!";
    System.out.println(message);
    return new ModelAndView("hello", "message", message);
   }
}

现在尝试点击http://localhost:port/appName/helloWorld/hello.html,如果您看到正确的页面,然后将链接更改如下

&lt;a href="/appName/helloWorld/hello.html"&gt;Say Hello&lt;/a&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多