您的位置:首页 > 编程语言 > Java开发

spring boot + jsp

2016-07-07 12:38 309 查看
创建一个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 Strin
4000
g 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;
}

@Override
public 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: .jsp
application.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
@ComponentScan
public class SampleWebJspApplication extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SampleWebJspApplication.class);
}

public static void main(String[] args) throws Exception {
SpringApplication.run(SampleWebJspApplication.class, args);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: