building services to work with forms
Code for this page is at :
https://github.com/gnurmatova/jersey-mvc-jstl-scopes
Create song service
create_song.jsp
<p>Create a Song</p>
<form method="POST" action="rest/song/mvc/create">
<input type="text" name="title"><br/>
<input type="text" name="artist"><br/>
<input type="submit">
</form>
service:
@POST
@Path("mvc/create")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response post(@FormParam("title") String title, @FormParam("artist") String artist) {
Song s = new Song();
s.setArtist(artist);
s.setTitle(title);
CreateSongCommand cmd = new CreateSongCommand();
return Response.ok(new Viewable("/create_success.jsp", cmd.execute(s))).build();
}
create_success.jsp:
Here is the response from the server:
${it}
https://github.com/gnurmatova/jersey-mvc-jstl-scopes
Create song service
create_song.jsp
<p>Create a Song</p>
<form method="POST" action="rest/song/mvc/create">
<input type="text" name="title"><br/>
<input type="text" name="artist"><br/>
<input type="submit">
</form>
service:
@POST
@Path("mvc/create")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response post(@FormParam("title") String title, @FormParam("artist") String artist) {
Song s = new Song();
s.setArtist(artist);
s.setTitle(title);
CreateSongCommand cmd = new CreateSongCommand();
return Response.ok(new Viewable("/create_success.jsp", cmd.execute(s))).build();
}
create_success.jsp:
Here is the response from the server:
${it}
View Song JSP only
view_song.jsp:
<%@ page import="model.*" %>
<%@ page import="command.*" %>
<%
GetSongCommand cmd = new GetSongCommand();
Song s = cmd.execute(Integer.parseInt(request.getParameter("id")));
out.print("<br/>Title: "+s.getTitle());
out.print("<br/>Artist: "+s.getArtist());
%>
<%@ page import="model.*" %>
<%@ page import="command.*" %>
<%
GetSongCommand cmd = new GetSongCommand();
Song s = cmd.execute(Integer.parseInt(request.getParameter("id")));
out.print("<br/>Title: "+s.getTitle());
out.print("<br/>Artist: "+s.getArtist());
%>
View Song Service
view_song2.jsp:
<%@ page import="model.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="song" value="${it.song}" scope="page"></c:set>
<br/>Title: ${song.getTitle()}
<br/>Artist: ${song.getArtist()}
service:
@GET
@Path("mvc/getsong")
public Response mvcGetSong(@QueryParam("id") int id) {
Map<String, Object> map = new HashMap<String, Object>();
GetSongCommand cmd = new GetSongCommand();
Song s = cmd.execute(id);
map.put("song", s);
return Response.ok(new Viewable("/view_song2.jsp", map)).build();
}
view_song2.jsp:
<%@ page import="model.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="song" value="${it.song}" scope="page"></c:set>
<br/>Title: ${song.getTitle()}
<br/>Artist: ${song.getArtist()}
service:
@GET
@Path("mvc/getsong")
public Response mvcGetSong(@QueryParam("id") int id) {
Map<String, Object> map = new HashMap<String, Object>();
GetSongCommand cmd = new GetSongCommand();
Song s = cmd.execute(id);
map.put("song", s);
return Response.ok(new Viewable("/view_song2.jsp", map)).build();
}