创建Action,使用@Controller注释
@RequestMapping(“hello”) 指定请求地址
@ResponseBody 向前端页面输出内容
spring直接将前台创递过来的数据,包装成一个JavaBean.
基本实例
先创建一个javaBean
package edu.hue.jk.bean;
import java.sql.Date;
public class pojo {
private Integer id;
private String post;
private Date hiredate;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
@Override
public String toString() {
return "pojo{" +
"id=" + id +
", post='" + post + '\'' +
", hiredate=" + hiredate +
'}';
}
}
MainApp
用SpringApplication
package edu.hue.jk;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainApp {
public static void main(String[] args) {
SpringApplication.run(MainApp.class);
}
}
Action
package edu.hue.jk.action;
import edu.hue.jk.bean.pojo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestAction {
@RequestMapping("hello")
@ResponseBody
public String hello(pojo p){
return "Hello World! you are a bad "+p.getPost()+"id="+p.getId()+"hiredate="+ p.getHiredate();
}
}
效果