Spring - 静态页面

时间:2016-12-09 05:34:38

标签: spring-mvc

我想显示静态页面,当我点击获取html页面时。它是spring mvc example.I有1个控制器和2个Jsp页面。它显示找不到页面并显示网址 的HelloWeb /的HelloWeb / staticPage?

WebController.java

 @Controller
  public class WebController {

 @RequestMapping(value = "/index", method = RequestMethod.GET)
 public String index() {
   return "index";
}
 @RequestMapping(value = "/staticPage", method = RequestMethod.GET)
public String redirect() {

  return "redirect:/pages/final.htm";
}
}

的web.xml

 <display-name>Spring Page Redirection</display-name>
    <servlet>
    <servlet-name>HelloWeb</servlet-name>
    <servlet-class>
       org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>HelloWeb</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

的HelloWeb-servlet.xml中

<context:component-scan base-package="com.tutorialspoint" />

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

<mvc:resources mapping="/pages/**" location="/WEB-INF/pages/" />
<mvc:annotation-driven/>

index.jsp

<head>
      <title>Spring Landing Page</title>
   </head>
    <body>
   <h2>Spring Landing Pag</h2>
  <p>Click below button to get a simple HTML page</p>
  <form:form method="GET" action="/HelloWeb/staticPage">
   <table>
      <tr>
         <td>
     <input type="submit" value="Get HTML Page"/>
     </td>
     </tr>
     </table>  
     </form:form>
     </body>

final.html

<head>
     <title>Spring Static Page</title>
     </head>
   <body>
    <h2>A simple HTML page</h2>
  </body>

1 个答案:

答案 0 :(得分:0)

您确定您的final.html正确放置为/pages/final.htm吗?

此外,在您的控制器中,您指定/staticPage作为RequestMapping的值。但是,在index.jsp中,您对表单的操作为/HelloWeb/staticPage。这两个不匹配。您确定项目路径设置为HelloWeb吗?

检查项目路径 enter image description here

尝试将表单的操作更改为/staticPage

即。 <form:form method="GET" action="/staticPage">