spring boot + jsp
来源:互联网 发布:淘宝手游交易平台官网 编辑:程序博客网 时间:2024/05/18 14:26
创建一个spring boot + jsp的工程
1. 首先创建一个maven工程boot-Spring-web:
整体目录结构:
2. 在pom文件导入以下依赖, 然后会自动导入相应的包:
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion><groupId>cn.boot.com</groupId> <artifactId>bootForSpring</artifactId> <version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>spring-web-demo</name><description>Demo project for Spring WebMvc</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.2.5.RELEASE</version><relativePath /></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope></dependency><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
3. 创建User.java实体类:
package cn.boot.com.bootForSpring.domain;public class User { private Long id; private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; if (id != null ? !id.equals(user.id) : user.id != null) return false; return true; } @Override public int hashCode() { return id != null ? id.hashCode() : 0; }@Overridepublic String toString() {return "User [id=" + id + ", name=" + name + "]";} }
创建controller:
TaskController.java: @Controller标签:返回jsp视图.package cn.boot.com.bootForSpring.controller;import java.util.Map;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestMapping;import cn.boot.com.bootForSpring.domain.User;/** * 返回jsp的视图 * @author Administrator * */@Controller@RequestMapping("/task")public class TaskController {/** * 用model传参 * @param model * @return */ @RequestMapping("/hello") public String greeting(Model model, @ModelAttribute("user") User user) { model.addAttribute("hahaha", "成功了"); System.out.println(user); return "helloWord"; } /** * 用map传参 * @param model * @return */ @RequestMapping("/helloWord") public String helloWord(Map<String, Object> model) { model.put("hahaha", "成功了"); return "helloWord"; }}
和UserController.java: @RestController标签:返回json.
package cn.boot.com.bootForSpring.web;import javax.servlet.http.HttpServletRequest;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import cn.boot.com.bootForSpring.domain.User;//@SpringBootApplication标签或@EnableAutoConfiguration都行(开启自动配置)//@SpringBootApplication@EnableAutoConfiguration@RestController @RequestMapping("/user") public class UserController { @RequestMapping("/{id}") public User view(@PathVariable("id") Long id, @ModelAttribute("user") User user1) { System.out.println(user1); User user = new User(); user.setId(id); user.setName("zhang"); return user; } @RequestMapping("/hello/{id}") public User hello(@PathVariable("id") Long id,User user, HttpServletRequest request) { System.out.println(user); System.out.println(request.getParameter("name")); return user; } @RequestMapping("/stringValue") public User hello(String str, HttpServletRequest request) { System.out.println(str); User user = new User(); return user; } public static void main(String[] args) { SpringApplication.run(UserController.class); } }
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=ISO-8859-1"><title>Insert title here</title></head><body>成功了<b />哈哈哈<b />${hahaha}<input type="submit" value="提交" id="tijiao"/></body><script src="/js/jquery-1.7.2.min.js"></script><script>$(function(){$('#tijiao').click(function(){alert("传数据");var param = {id : 1, name : '哈哈'};var url = "http://localhost:8080/task/hello";$.ajax({ type:"get", url:url, data: param, dataType:'html', cache : false, async: false, success:function(data){ if(data.code == "000000"){ layer.alert('操作成功'); }else{ layer.alert('操作失败'); } }})});});</script></html>
Js位置:
application.properties文件:
spring.view.prefix: /WEB-INF/jsp/spring.view.suffix: .jspapplication.message: Hello Phil
SampleWebJspApplication.java:启动服务器(main()方法启动):
package cn.boot.com.bootForSpring;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.context.web.SpringBootServletInitializer;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@EnableAutoConfiguration@ComponentScanpublic class SampleWebJspApplication extends SpringBootServletInitializer {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(SampleWebJspApplication.class);}public static void main(String[] args) throws Exception {SpringApplication.run(SampleWebJspApplication.class, args);}}
0 0
- Spring Boot JSP
- spring boot jsp
- spring boot + jsp
- Spring boot-jsp
- spring boot tomcat jsp
- Spring Boot JSP 404
- spring-boot-jsp
- Spring Boot+JSP
- spring boot之jsp
- Spring Boot 集成JSP
- Spring Boot与Jsp
- Spring-Boot使用jsp
- Spring Boot (二) Spring Boot for JSP
- spring-boot,jdbc,jsp配置
- Spring-Boot 与jsp/freemarkerDemo
- Spring Boot 添加JSP支持
- Spring Boot 添加JSP支持
- Spring boot(11) 使用jsp
- Window_纪中1326_水_单调队列
- Party_纪中_1328
- java匹配IP正则表达式
- HTML网页开发 燕十八 第6课学习笔记-浮动布局
- 动态规划算法
- spring boot + jsp
- linux 修改密码(二)
- Android自定义控件之仿京东商城下拉刷新
- 前端打包构建工具gulp入门实战
- 20160707 在Ubuntu Linux上安装搜狗输入法及对Qt Creator的支持
- AIX运维常用命令
- bzoj1251 序列终结者
- Servlet中的编码
- 对dp的理解