package com.sv.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 页面展示
* @author jixh
*
*/
@Controller
public class PageController {
/**
* 展示首页
* @return
*/
@RequestMapping(value="/")
public String indexShow() {
return "index";
}
/**
* 展示其他页面,每一个页面请求都要写一个对应的页面返回处理,这样太过麻烦,进行简化
* 方法:正如所见,将请求的URL和处理函数返回的URL写成一样的,这样一来就可以使用
* Restfaull的URL模板映射,直接将请求的URL作为返回的URL, 如此就可公用了
* 如果参数名称和表达式名称不一致时,加参数(@PathVariable(value="page") String page3)
*
*/
@RequestMapping(value="/{page}")
public String pageShow(@PathVariable String page) {
return page;
}
}
package com.huawei.myconfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration//标志当前类在启动的时候会被加载
public class MyConfiguration implements WebMvcConfigurer {
//视图控制器配置
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//urlPath:浏览器输入的地址 viewName:该地址跳转到的页面,不加templates文件夹和.html
//registry.addViewController("/left").setViewName("left");错误的写法
registry.addViewController("/login").setViewName("left.html");
}
}