官术网_书友最值得收藏!

How to do it...

You need to perform the following steps to try this recipe:

  1. Start by creating a root for your JAX-RS endpoints:
@ApplicationPath("webresources")
public class AppConfig extends Application{
}
  1. 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
}
  1. 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");
}
}
  1. 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";
}
}
  1. And finally, create 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>
  1. Run it on a Jakarta EE 8 server and access this URL:
http://localhost:8080/ch01-mvc/webresources/userController

主站蜘蛛池模板: 灵山县| 红安县| 宁南县| 太原市| 曲靖市| 乡城县| 黄梅县| 定远县| 高州市| 阳曲县| 天等县| 玛沁县| 岑溪市| 汉川市| 香港| 太保市| 和田县| 荣昌县| 鄂温| 定南县| 镇巴县| 牟定县| 南开区| 张家口市| 武功县| 兴隆县| 黑河市| 利津县| 华池县| 文化| 涿鹿县| 班玛县| 永和县| 利辛县| 太湖县| 台南县| 平罗县| 得荣县| 寿阳县| 大安市| 辽宁省|