标签:user 端口 后缀 use ima attr -- request ret
创建一个springBoot工程
然后输入工程名字
然后选择要导入的依赖jar包,这里只勾选了web
然后目录结构
创建一个controller必须在aplication.properties的同级目录下或者子级目录下,
package com.example.demo.controller; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController //相当于 @Controller @ResponseBody 这两个 自动转换为json格式,无法返回jsp页面 public class DemoController { @RequestMapping("json/demo") public String demoString(){ return "hello world"; } @RequestMapping("map") public Map<String, String> demoMap(){ Map<String,String> map= new HashMap<String,String>(); map.put("userName", "张三"); map.put("passWord", "123"); return map; } @RequestMapping("list") public List<String> demoList(){ ArrayList<String> list = new ArrayList<String>(); list.add("张三"); list.add("list"); return list; } }
然后如果要想访问自己写的jsp,必须在pom.xml中导入jsp依赖
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <dependency> <groupId>org.eclipse.jdt.core.compiler</groupId> <artifactId>ecj</artifactId> <version>4.6.1</version> <scope>provided</scope> </dependency>
然后在埋目录下必须有webapp---->WEB-INF目录,在web-inf目录下编写jsp页面,在这里是建立了一个jsp的包
然后编写jsp页面,上面的图片中index.jsp就是编写的
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> this is index page ${message } </body> </html>
然后配置application.properties
spring.mvc.view.prefix 表示视图前缀
spring.mvc.view.suffix 表示视图后缀
server.port=8080 表示端口号 8080
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
server.port=8080
然后编写jspcontroller
package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class JspController { @RequestMapping("jsp/index") public String index(Model model){ model.addAttribute("message", "hello wrold"); return "index"; } }
标签:user 端口 后缀 use ima attr -- request ret
原文地址:http://www.cnblogs.com/jiangxiangit/p/7296569.html