spring boot通过ip+端口直接访问页面

看demo结构

spring boot通过ip+端口直接访问页面_第1张图片

有两种方式

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

spring boot通过ip+端口直接访问页面_第2张图片   spring boot通过ip+端口直接访问页面_第3张图片    spring boot通过ip+端口直接访问页面_第4张图片

 

 

 

 

 

你可能感兴趣的:(spring,boot)