Welcome file not working with html file in spring

一世执手 提交于 2019-11-29 11:03:32

Try adding <mvc:default-servlet-handler/> in your dispatcher-servlet.xml.

See here for details.

You have mapped all your incoming requests to the dispatcher here,

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

So all your URL requests for the application goes inside the dispatcher as '/' maps all incoming requests . check for the stacktraces in your application server log

update:

You get the below warning because there are no handler for the '/' pattern,

WARNING: No mapping found for HTTP request with URI [/AccelFlow/] in DispatcherServlet with name 'dispatcher'

You can do either of below options ,

  1. Map a url with '/' to the controller
  2. Add a specific URL pattern to the spring dispatcher such as .htm or .do as you wish

Modify your web.xml,

<servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>  

And in your controller,

@RequestMapping(value = "/test.htm", method = RequestMethod.GET)
public @ResponseBody Response display() throws Exception {
    accelFlowFacade.disaply();
    Response res = new Response();
    return res;
}
Saifulcse01

At the startup by default all incoming requests are mapping to '/' pattern as you write in the web.xml:

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

update:

  1. Try to map a Controller method for the default view:

    @RequestMapping(value = "/", method = GET)
    public String welcome() {
        return "index";
    }
    
  2. Add viewresolver to dispather-servlet.xml:

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/"
          p:suffix=".jsp" />
    
  3. Remove welcome file from the web.xml as automatically spring will search for index page by default:

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    

Welcome file can be access using following changes.

change 1. add resource path in dispatcher as following :

      <mvc:resources mapping="/" location="/index.html" />

change 2. add controller handler like following :

     @Controller
      public class RestController {

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

     }

changes 3: index.html file should be in WebContent folder in project.

Note : If you are not able to add mvc bean in dispatcher servlet file then add

  xmlns:mvc="http://www.springframework.org/schema/mvc

in dispatcher servlet config file.

using <mvc:resources mapping="/" location="/index.html" /> is goods for static pages , however changing it to

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

is not good design as all model in other controllers may point back to index page, instead use

<mvc:resources mapping="/" location="/redfresh.html"  />

and make refresh page such

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <meta http-equiv="refresh" content="0;URL='/index'" />
</head>
<body>
</body>
</html>

and point in controller to index such:

@Controller
public class indexPageController {

 @RequestMapping(value = "/index", method = RequestMethod.GET, produces = "text/html")
    public String index() {       
        return "index";
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!