Welcome file not working with html file in spring

前端 未结 6 1176
难免孤独
难免孤独 2020-12-19 10:09

I have given my welcome file in web.xml But when running the application, it is showing 404 error on http://172.16.2.16:8080/sampletest/

It is a spring

相关标签:
6条回答
  • 2020-12-19 10:10

    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.

    0 讨论(0)
  • 2020-12-19 10:23

    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;
    }
    
    0 讨论(0)
  • 2020-12-19 10:23

    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>
      
    0 讨论(0)
  • 2020-12-19 10:26

    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";
        }
    }
    
    0 讨论(0)
  • 2020-12-19 10:28

    Just add your index.html to webContent folder. Because welcome file is searched in that folder itself.

    0 讨论(0)
  • 2020-12-19 10:34

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

    See here for details.

    0 讨论(0)
提交回复
热议问题