AngularJS, UI Bootsrap, MVC Frontend - data-ng-click is not working - java

I am trying to register user (take some information from form input at frontend and send it to backend via AngularJS) but I am experiencing some problems.
Here is my index.html file:
<!DOCTYPE html>
<html data-ng-app="collectionsApp">
<head>
<meta charset="UTF-8">
<title>Collections</title>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div data-ng-view></div>
</body>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.10.0/ui-bootstrap-tpls.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular-route.js"></script>
<script src="/scripts/app.js"></script>
<script src="/scripts/service/userService.js"></script>
<script src="/scripts/controller/indexController.js"></script>
<script src="/scripts/controller/registerController.js"></script>
</html>
As you ca see, I have included Bootstrap UI, AngularJS, Bootstarp CSS and my project files: userService.js, indexController.js and registerController.js. I have created atributes data-ng-app="collectionsApp" (created in app.js) and data-ng-view (routing works just fine).
Here is my app.js file:
var collectionsApp = angular.module('collectionsApp', [ 'ngRoute' ]);
collectionsApp.config(function($routeProvider) {
$routeProvider.when('/login', {
templateUrl : '/view/login.html',
controller : 'loginController'
})
.when('/register', {
templateUrl : '/view/register.html',
controller : 'registerController'
}).
otherwise({
redirectTo : '/login'
});
});
As you can see I have included my registerControll and loginControll in app.js so i do not have to do it in register.html and login.html.
Here is my register.html file:
<div class="container">
<form class="form-signin" role="form">
<h1 class="form-signin-heading">Please register</h1>
<input type="text" class="form-control" placeholder="Email address" required data-ng-model='user.email'>
<input type="text" class="form-control" placeholder="Password" required data-ng-model='user.password'>
<input type="text" class="form-control" placeholder="Firts name" required data-ng-model='user.firstName'>
<input type="text" class="form-control" placeholder="Last name" required data-ng-model='user.LastName'>
<button class="btn btn-lg btn-primary btn-block" type="submit" data-ng-click="register(user)">Register</button>
<div>
Login
</div>
</form>
</div>
On data-ng-click register(user) function is called from userControl.js file.
Here is my userController.js file:
collectionsApp.controller('registerController', function($scope, userService) {
userService.register($scope.user).then(function(data){
});
});
From this file, function from userService.js file is called.
Here is my userService.js file:
collectionsApp.service('userService', function($scope, $http) {
return {
login : function(user) {
$http.post('/user/login', user).then(function(data) {
return data;
});
},
register : function(user) {
$http.post('/user/add', user).then(function(data) {
return data;
});
}
};
});
From here register function targets /user/add in userController.java in backend. Here is that file:
package x.y.collections.controller;
import java.util.HashMap;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import x.y.collections.dao.UserDAO;
import x.y.collections.model.User;
import x.y.collections.utility.Response;
#RestController
#RequestMapping("/user")
public class UserController {
#Resource
UserDAO userDao;
#RequestMapping("/find/{id}")
public User FindUser(#PathVariable Long id) {
return userDao.findOne(id);
}
#RequestMapping(value = "/add", method = RequestMethod.POST)
public HashMap<String, String> AddUser(#RequestBody User user) {
final User requestedUserBody = userDao.findByEmail(user.getEmail());
if (requestedUserBody.getEmail().equals(user.getEmail())) {
return Response.setError("User allready in databse!");
} else {
userDao.save(user);
return Response.setSuccess("Registration succsessful!");
}
}
#RequestMapping(value = { "/login" }, method = RequestMethod.POST)
public HashMap<String, String> LoginUser(#RequestBody User user) {
final User requestedUserBody = userDao.findByEmail(user.getEmail());
if (requestedUserBody.getPassword().equals(user.getPassword())) {
return Response.setError("Username and password do not match!");
} else {
return Response.setSuccess("Login succsessful!");
}
}
}
When I start server and go to my frontend via Chrome and localhost:port in adressbar I can see that routing is working, but when I fill in the form, and press button that is supposed to trigger data-ng-click="register(user)" noting happens. When I go right click > inspect element > console : I see this error:
Error: [$injector:unpr] http://errors.angularjs.org/1.2.19/$injector/unpr?p0=<div data-ng-view="" class="ng-scope">copeProvider%20%3C-%20%24scope%20%3C-%20userService
at Error (native)
at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js:6:450
at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js:36:202
at Object.c [as get] (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js:34:305)
at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js:36:270
at c (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js:34:305)
at d (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js:35:6)
at Object.instantiate (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js:35:165)
at Object.<anonymous> (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js:35:435)
at Object.d [as invoke] (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js:35:36)
Can someone explain to me what I am doing wrong?
Than you in advance...
Papi

In your registerController
collectionsApp.controller('registerController', function($scope, userService) {
$scope.user = {};
.....
$scope.register = function(){
userService.register($scope.user).then(function(data){
});
};
});

Related

Problem with updating "movie" data using Thymeleaf and spring

Hey I've got a problem with updating my object. I've got a table of movies where can i delete it or go to details. In details i wanted to have my "update" option. Im giving whole object to movieDetailPage, it shows object details in input fields but i cant update it. My "updateMovie" method doesnt see what i typed in input. For example im changing name from "Movie1" to "Movie2" and method still see "Movie1"
Here's my movieDetailsPage.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:object="${movieToUpdate}" method=post >
<p>Movie name: <input type="text" th:field="*{name}"></p>
<p>Movie description: <input type="text" th:field="*{description}"></p>
<img th:src="${movieToUpdate.getImageUrl()}" th:width="300" th:height="300"><br>
<p>Nowy image url: <input type="text" th:field="*{imageUrl}" class="cloudinary-fileupload"></p>
<a th:href="${'/movie/update/'+movieToUpdate.getId()}">Update</a><br>
</form>
</body>
</html>
and my controller class
package pl.fsaaa.filmapp.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import pl.fsaaa.filmapp.model.Movie;
import pl.fsaaa.filmapp.service.CloudinaryImageUploader;
import pl.fsaaa.filmapp.service.MovieService;
import java.io.IOException;
import java.util.List;
#Controller
public class MovieController {
private CloudinaryImageUploader cloudinaryImageUploader;
private MovieService movieService;
#Autowired
public MovieController(MovieService movieService, CloudinaryImageUploader cloudinaryImageUploader) {
this.movieService = movieService;
this.cloudinaryImageUploader = cloudinaryImageUploader;
}
#GetMapping("/addmovie")
public String getNewMovie(Model model) {
model.addAttribute("newMovie",new Movie());
// model.addAttribute("newImage",new File(""));
return "addMoviePage";
}
#PostMapping("/add-movie")
public String addMovie(#ModelAttribute Movie movie) throws IOException {
cloudinaryImageUploader.saveImageToCloudinary(movie.getImageUrl());
movie.setImageUrl(cloudinaryImageUploader.getCloudinaryImageUrl());
movieService.addMovie(movie);
return "redirect:/addmovie";
}
#GetMapping("/movies")
public String getAllMovies(Model model) {
List<Movie> movieList = movieService.getAllMovies();
model.addAttribute("allMovies",movieList);
return "moviesPage";
}
#GetMapping("/movie/{id}")
public String getMovieDetail(Model model, #PathVariable Long id) {
Movie movieToUpdate = movieService.getMovieById(id);
System.out.println(movieToUpdate);
model.addAttribute("movieToUpdate", movieToUpdate);
return "movieDetailsPage";
}
#RequestMapping(value = "/movie/update/{id}", method = {RequestMethod.GET,RequestMethod.PUT})
// #PutMapping("movie/update/{id}")
public String updateMovie(#PathVariable Long id, #ModelAttribute Movie movieToUpdate){
// movieToUpdate = movieService.getMovieById(id);
// System.out.println(movieToUpdate);
movieService.addMovie(movieToUpdate);
return "redirect:/movies";
}
#RequestMapping(value = "/movie/delete/{id}", method = {RequestMethod.DELETE,RequestMethod.GET})
public String deleteMovie(#PathVariable Long id){
movieService.deleteMovie(id);
return "redirect:/movies";
}
}
Problem solved.
I've added submit button to details page instead of link:
<form th:action="'/movies/update/'+*{id}" th:object="${movieToUpdate}" method=put >
<p>Movie name: <input type="text" th:field="*{name}"></p>
<p>Movie description: <input type="text" th:field="*{description}"></p>
<img th:src="${movieToUpdate.getImageUrl()}" th:width="300" th:height="300"><br>
<p>New image url: <input type="text" th:field="*{imageUrl}" class="cloudinary-fileupload"></p>
<p><input type="submit" value="Update"></p>
</form>
and changed update edited "movie" method a little bit, because previous one wasn't uploading changed image to cloudinary.
#RequestMapping(value = "/update/{id}", method = {RequestMethod.GET,RequestMethod.PUT})
public String updateMovie(#PathVariable Long id, #ModelAttribute Movie movieToUpdate) throws IOException {
movieToUpdate.setId(id);
cloudinaryImageUploader.saveImageToCloudinary(movieToUpdate.getImageUrl());
movieToUpdate.setImageUrl(cloudinaryImageUploader.getCloudinaryImageUrl());
movieService.addMovie(movieToUpdate);
return "redirect:/movies/all";
}

Successful login with Custom login page in spring boot starter security not getting to next page

I have been studying spring boot. When using spring-boot-starter-security for a todo application,I tried to login with custom user id and password and custom login page.When I tried login , it is not taking me to next page. Note : user name is required parameter for next page.
I tried using below but after login it again takes me to login page as error
#Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/login").permitAll()
.antMatchers("/", "/*Todo*/**").access("hasRole('USER')").and()
.formLogin().loginPage("/login").permitAll();
}
This is my securityConfig code
#Configuration
#EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
#Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/login").permitAll()
.antMatchers("/listTodo").hasAnyRole("USER","ADMIN")
.anyRequest().authenticated()
.and().formLogin().loginPage("/login").permitAll().and()
.logout().permitAll();
}
#Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("Sudhakar").password("qwe123").roles("USER","ADMIN");
}
}
I would need to login with user name and get the todo details of the user who logged in. But I am not able to get to next page, after trying many times I am getting below error
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
Below is my controller
package com.example.SpringLogin.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.example.SpringLogin.service.loginService;
#Controller
#SessionAttributes("name")
public class LoginController {
#Autowired
loginService service;
#RequestMapping(value="/login",method = RequestMethod.POST)
public String loginMessage(ModelMap model,#RequestParam String name,#RequestParam String password) {
boolean isValidUser=service.validateUser(name, password);
if(!isValidUser) {
model.put("message", "Invalid Credentials");
return"Room";
}
model.put("name", name);
model.put("password",password);
return "redirect:/todoList";
}
#RequestMapping(value="/login",method = RequestMethod.GET)
public String roomLogin(ModelMap model, String error) {
//model.put("name", name);
if(error!=null) {
model.addAttribute("errorMsg","UserName or Password is invalid");
}
return "Room";
}
/*#RequestMapping(value="/login",method = RequestMethod.GET)
public String showLogin(ModelMap model) {
//model.put("name", name);
return "Welcome";
}*/
#RequestMapping(value = "/welcome")
public String showWelcome(ModelMap model) {
return "login";
}
}
My login page
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<c:set var="contextPath" value=""/>
<!DOCTYPE html>
<html>
<head>
<link href="webjars/bootstrap/3.3.6/css/bootstrap.min.css"
rel="stylesheet">
<title>Todo Application</title>
</head>
<body>
<div class="container">
<font color="red">${message}</font>
<form:form method="post" action="/login">
<fieldset class="form-group">
Name : <input type="text" name="username" class="form-control" placeholder="Username"
autofocus="true"/>
Password: <input type="password" name="password"
class="form-control" placeholder="Password" />
</fieldset>
<button type="submit" class="btn btn-success">Submit</button>
</form:form>
</div>
<script src="webjars/jquery/1.9.1/jquery.min.js"></script>
<script src="webjars/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</body>
</html>
After successful login it should go to below page
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<c:set var="contextPath" value=""/>
<!DOCTYPE html>
<html>
<head>
<link href="webjars/bootstrap/3.3.6/css/bootstrap.min.css"
rel="stylesheet">
<title>Todo Application</title>
</head>
<body>
<div class="container">
<table class="table table-striped">
<H1>Name : ${pageContext.request.userPrincipal.name}</H1>
<thead>
<tr>
<th>Id</th>
<th>Course</th>
<th>End Date</th>
<th>Is it Done</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<c:forEach items="${todo}" var="item">
<tr>
<td>${item.id}</td>
<td>${item.course}</td>
<td><fmt:formatDate value="${item.date}" pattern="MM/dd/yyyy" /></td>
<td>${item.isdone?'Yes':'No'}</td>
<td><a type="button" class="btn btn-success"
href="/update-Todo?id=${item.id}">Update</a></td>
<td><a type="button" class="btn btn-warning"
href="/delete-Todo?id=${item.id}">Delete</a></td>
</tr>
</c:forEach>
</tbody>
</table>
<div>
<a type="button" href="/add-Todo" class="btn btn-success">Add a
Todo</a>
</div>
</div>
<script src="webjars/jquery/1.9.1/jquery.min.js"></script>
<script src="webjars/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</body>
</html>
Service class
package com.example.SpringLogin.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Service;
import com.example.SpringLogin.model.todoList;
#Service
public class TodoService {
public static List<todoList> todos=new ArrayList<todoList>();
public static int todoCount=5;
static {
todos.add(new todoList(1, "Sudhakar", "Study history", new Date(), false));
todos.add(new todoList(2,"Sudhakar","Study geography",new Date(),false));
todos.add(new todoList(3,"Sudhakar","Study GK",new Date(),false));
todos.add(new todoList(4,"Mani","Study Java",new Date(),false));
todos.add(new todoList(5,"Mani","Study script",new Date(),false));
}
public List<todoList> retrievetodos(String name){
List<todoList> retrieved=new ArrayList<todoList>();
for (todoList todo : todos) {
if(todo.getName().equalsIgnoreCase(name)) {
retrieved.add(todo);
}
}
return retrieved;
}
public void addTodo(String name,String Course,Date date,boolean isDone) {
todos.add(new todoList(++todoCount,name,Course,date,isDone));
}
public todoList retrieveTodo(int id){
for (todoList todo : todos) {
if(todo.getId()==id) {
return todo;
}
}
return null;
}
public List<todoList> UpdateTodo(todoList todo){
/*for (todoList td : todos) {
if(td.getId()==todo.getId()) {
td.setCourse(todo.getCourse());
td.setDate(todo.getDate());
}
}*/
todos.remove(todo);
todos.add(todo);
return todos;
}
//it will delete the todo
public void deleteTodo(int id) {
Iterator<todoList> it = todos.iterator();
while(it.hasNext()){
todoList td=it.next();
if(td.getId()==id) {
it.remove();
}
}
}
}
my expectation is to login application using user name and get the todolist of the user
Try adding a loginProcessUrl and a defaultSuccessUrl. Something like this:
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/do_login")
.defaultSuccessUrl("/index")
index in this example is the page you want to be taken to upon successful login.
Use this one
.formLogin()
.loginPage("/login.html")
.loginProcessingUrl("/perform_login")
.defaultSuccessUrl("/homepage.html", true)

Hello world using AJAX and SPRING not working

I am trying make hello world using ajax and spring, but it not workit, I dont know why.
My servlet is:
import javax.servlet.http.HttpServlet;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
#Controller
public class SomeController {
#RequestMapping(value = "/profile", method = RequestMethod.GET)
public #ResponseBody
String processAJAXRequest(
#RequestParam("firstname") String firstname,
#RequestParam("lastname") String lastname) {
String response = "HELLO: " + firstname + " " + lastname;
return response;
}
}
And my page with ajax is:
<html>
<head>
<title>TODO supply a title</title>
<meta charset="windows-1250">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<form id="sampleForm" method="post" action="/profile">
<div>
<input type="text" name="firstname" id="firstname">
</div>
<div>
<input type="text" name="lastname" id="lastname">
</div>
<div>
<button type="submit" name="submit">Submit</button>
</div>
</form>
<script>
$(document).ready(function () {
$('#sampleForm').submit(
function (event) {
var firstname = $('#firstname').val();
var lastname = $('#lastname').val();
var data = 'firstname='
+ encodeURIComponent(firstname)
+ '&lastname='
+ encodeURIComponent(lastname);
$.ajax({
url: $("#sampleForm").attr("action"),
data: data,
type: "GET",
success: function (response) {
alert(response);
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
});
return false;
});
});
</script>
</body>
</html>
When I try to send form, I am getting error 404. Can you help me? Where is a problem?
Normal servlet extends from HttpServlet working fine.

Building Restful Web Service using Spark Java for Library Management System

I am new with Spark Java. I have made a Library Management System with get and post methods written in Java. I have made an HTML form that takes information of user and creates users which gets stored in a ArrayList of objects in Java. When I call get method to return the list of user objects created via ajax, nothing gets returned. Success nor error gives any output.
But when I simply put the url : http://localhost:4567/users
I get the following output:
[{"id":1,"firstName":"Bhavya","lastName":"Chauhan","age":"22","gender":"F","phone":"1234567890"},
{"id":2,"firstName":"Rashi","lastName":"Chauhan","age":"20","gender":"F","phone":"1233455677"}]
I have attached my html and Spark java code. Thanks in advance!
<!DOCTYPE html>
<html>
<head>
<title>Text Input Control</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<meta charset="utf-8"/>
</head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript"></script>
<script>
$(function () {
$("#userGet").click(function(){
alert("Here");
$.ajax({
type: 'GET',
url: 'http://localhost:4567/users',
crossDomain: true,
data: '',
dataType: 'jsonp',
async:false,
success: function(responseData) {
//$("#divid").html(+responseData);
alert("hi");
},
error: function(jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
});
});
</script>
<body>
<h2>Library Management System </h2>
<button>Get Stuff</button>
<form>
<h3>Manage Users </h3>
<input type=button onClick="location='CreateUser.html'" value='Create User'>
<br><br>
<input type=submit id="userGet" value='Get All Users'>
<br><br>
<input type=button onClick="location='UpdateUser.html'" value='Update User'>
<br><br>
</form>
<form action="http://localhost:4567/books" method="GET">
<h3>Manage Books </h3>
<input type=submit value='Get All Books'>
<br><br>
<input type=button onClick="location='FindBookByName.html'" value='Find Book By Name'>
<br><br>
<input type=button onClick="location='AddBook.html'" value='Add Book'>
<br><br>
<input type=button onClick="location='CheckOutBook.html'" value='Check Out Book'>
<br><br>
</form>
<div id="divid"></div>
</body>
</html>
Here is the Java:
import static spark.Spark.*;
public class UserController extends JsonUtil
{
public UserController(final UserService userService)
{
System.out.println("in user controller");
get("/users", (req, res) -> userService.getAllUsers(), json());
get("/books", (req, res) -> userService.getAllBooks(), json());
post("/users", (req, res) -> userService.createUser(req.body()), json());
post("/users/:id", (req, res) -> userService.updateUser(req.body()),json());
post("/books", (req, res) -> userService.addBook(req.body()), json());
post("/books/:name", (req, res) -> userService.findBookByName(req.body()),json());
post("/checkedOut", (req, res) -> userService.checkOutBook(req.body()), json());
}
}

consuming array of json object in jersey in java

to consume a single object in jersey I have my method like this
#POST
#Consumes(MediaType.APPLICATION_FORM_URLENCODED)
#Produces(MediaType.APPLICATION_JSON)
public Response postPerson(MultivaluedMap<String, String> personParams){
ResponseBuilder response = Response.ok();
return response.build();
}
its working well.
my doubt is how can i consume list of objects in jersey
some thing like this
#POST
#Consumes(MediaType.APPLICATION_FORM_URLENCODED)
#Produces(MediaType.APPLICATION_JSON)
public Response postPerson(List<Person> person){
ResponseBuilder response = Response.ok();
return response.build();
}
I did some googling but didn't found any working example.
It seems you want to pass the form data as list:
#POST
#Consumes(MediaType.APPLICATION_FORM_URLENCODED)
#Produces(MediaType.APPLICATION_JSON)
public Response postPerson(#FormParam("person") List<String> person){
ResponseBuilder response = Response.ok();
return response.build();
}
<html><body>
<form action="http://localhost:9998/myresource" method="POST">
<input type="checkbox" name="person" value="a">A</input>
<input type="checkbox" name="person" value="b">B</input>
<input type="checkbox" name="person" value="c">C</input>
<input type="submit">OK</input>
</form>
</body></html>
similarly for Person object, you have to map the diffrent property so that it can post the list of person
Source:JERSEY RESTful - How to work with multiselect checkboxes? this has worked for me.
I am using jersy as the RESTful webservice and angularJS to post all the todo objects at the same time use your person object to update data at the same time
//Define an angular module for our app
var sampleApp = angular.module('sampleApp', ['ngRoute','ngResource']);
sampleApp.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/AddNewOrder', {
templateUrl: 'add_order.jsp',
controller: 'AddOrderController'
}).
when('/TestArray', {
templateUrl: 'list.html',
controller: 'TestArrayController'
}).
}).
otherwise({
redirectTo: '/AddNewOrder'
});
}]);
sampleApp.controller( 'TestArrayController', function ( $scope, $location,$http,$resource) {
var array=new Array();
var postObject1 = new Object();
postObject1.description = "testDesc1";
postObject1.summary = "testSummary1";
array.push(postObject1);
var postObject2 = new Object();
postObject2.description = "testDesc2";
postObject2.summary = "testSummary2";
array.push(postObject2);
$http.post("/AngularJS/JerseyWebService/todo/arrayTest", array).success(function(data){
//Callback function here.
//"data" is the response from the server.
$scope.updateMessage=data;
});
HTML page
<html lang="en">
<head>
<title>AngularJS Routing example</title>
<script src="jquery.min.js" type="text/javascript" language="javascript"></script>
<script src="xml2json.js" type="text/javascript" language="javascript"></script>
<script src="angular.js"></script>
<script src="angularjs/angular-route.js"></script>
<script src="angularjs/angular-resource.js"></script>
<script src="app1.js"></script>
<script type="text/javascript">
var jquery = $.noConflict();
</script>
</head>
<body ng-app="sampleApp">
<div class="container">
<div class="row">
<div class="col-md-3">
<ul class="nav">
<!--<li> Add New Order </li>
<li> Show Orders</li>-->
<li>Test Array Update</li>
</ul>
<div>{{updateMessage}}</div>
</div>
<div class="col-md-9">
<div ng-view></div>
</div>
</div>
</div>
</body>
</html>
Jersy WebService
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
#Path("/todo")
public class TodoResource {
#POST
#Path("/arrayTest")
#Consumes({ MediaType.APPLICATION_JSON })
#Produces({ MediaType.APPLICATION_JSON })
public String wantsJSONArray(List<Todo> array)
{
System.out.println("---------------------------------------------------"+array);
// here's your array
return "success";
}
}
Todo.java
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
// JAX-RS supports an automatic mapping from JAXB annotated class to XML and JSON
// Isn't that cool?
public class Todo {
private String summary;
private String description;
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String toString(){
return "{summary:"+summary+"description:"+description+"}";
}
}
web.xml entry
<servlet>
<servlet-name>JerseyWebService</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JerseyWebService</servlet-name>
<url-pattern>/JerseyWebService/*</url-pattern>
</servlet-mapping>

Categories

Resources