上QQ阅读APP看书,第一时间看更新
How to do it...
- Start by creating a root for your JAX-RS endpoints:
@ApplicationPath("webresources")
public class AppConfig extends Application{
}
- Create a User class (this will be your MODEL):
public class User {
private String name;
private String email;
public User(String name, String email) {
this.name = name;
this.email = email;
}
//DON'T FORGET THE GETTERS AND SETTERS
//THIS RECIPE WON'T WORK WITHOUT THEM
}
- Now, create a Session Bean, which will be injected later in your Controller:
@Stateless
public class UserBean {
public User getUser(){
return new User("Elder", "elder@eldermoraes.com");
}
}
- Then, create the Controller:
@Controller
@Path("userController")
public class UserController {
@Inject
Models models;
@Inject
UserBean userBean;
@GET
public String user(){
models.put("user", userBean.getUser());
return "/user.jsp";
}
}
- And finally, the web page (the View):
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>User MVC</title>
</head>
<body>
<h1>${user.name}/${user.email}</h1>
</body>
Run it on a Java EE 8 server and access this URL:
http://localhost:8080/ch01-mvc/webresources/userController