spring boot通過ip+端口直接訪問頁面

看demo結構

有兩種方式

1.

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class userController {
    @RequestMapping("/")
    public String hello(ModelMap map) {

        return "index";
    }
}

2.

import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

public class DefultView extends WebMvcConfigurerAdapter {

    public void addViewControllers(ViewControllerRegistry registry){
        registry.addViewController("/").setViewName("forward:/index.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        super.addViewControllers(registry);
    }
}

 

瀏覽器輸入http://localhost:8080  或者 http://127.0.0.1:8080 或者 http://192.168.4.123:8080    即可訪問index.html

       

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章