I need send a form from php to java using the method post and curl.
I'm having problems to fetch a list of objects in php when I submit the form. I would like to know if is possible to do it like in Spring MVC (specifying attribute name in the form)
<form method="post" action="group_create.php">
<div>
Name: <input type="text" name="name" />
<br />
<input type="text" name="users[0].name" value="753" />
<br />
<input type="text" name="users[1].name" value="752" />
<br />
<input type="submit" value="Save" />
</div>
</form>
Encoding method:
<?php
http_build_query($_POST);
?>
Current curl url:
javaserver/app/groups/create?name=Administrator&users[0]=Tom&users[1]=Myke
Necessary curl url:
javaserver/app/groups/create?name=Administrator&users[0].name=Tom&users[1].name=Myke
My DTO's:
public class GroupDTO {
private String name;
private List<UserDTO> users;
public GroupDTO()
{
this.users = new ArrayList<UserDTO>();
}
}
public class UserDTO {
private String name;
}
Spring MVC Controller:
#Controller
#RequestMapping("secure/groups")
public class GroupsController {
#RequestMapping(value = "create", method = RequestMethod.POST)
public #ResponseBody ModelMap create(GroupDTO dto)
{
ModelMap map = new ModelMap();
//CODE TO DO ANYTHING
return map;
}
}
Change this:
<form method="post" action="group_create.php">
<div>
Name: <input type="text" name="name" />
<br />
<input type="text" name="users[0].name" value="753" />
<br />
<input type="text" name="users[1].name" value="752" />
<br />
<input type="submit" value="Save" />
</div>
</form>
To this:
<form method="post" action="group_create.php">
<div>
Name: <input type="text" name="name" />
<br />
<input type="text" name="users[0]" value="753" />
<br />
<input type="text" name="users[1]" value="752" />
<br />
<input type="submit" value="Save" />
</div>
</form>
The ".name" part is not needed for the form to operate properly.
Related
Cuba has its own backend system to add users.
Now I need to write a user registration in the front-end.
The Cuba version I am using is 6.9
I know that this version of encryption is SHA1 : https://doc.cuba-platform.com/manual-6.9/login.html
Now my question is : I don't know how to set the encrypted password to the database.
I create an entity through the Metadata
User user = metadata.create(User.class);
user.setPassword(passWord);
I'm not sure that's the best option but I used the following code:
#Inject
protected PasswordEncryption passwordEncryption;
...
user.setPassword(passwordEncryption.getPasswordHash(user.getId(), password));
I think you just need to create html file with this code:
<div class="span6">
<h3>Login</h3>
<div th:if="${(param.error != null)}">
<p>Invalid username / password</p>
</div>
<form id="f" name="f" method="POST" action="login">
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
<div>
<div>
<label style="width: 80px" for="login">Login</label>
<input type="text" id="login" name="login" value=""/>
</div>
<div>
<label style="width: 80px" for="password">Password</label>
<input type="password" id="password" name="password" value=""/>
</div>
</div>
<button type="submit">Login</button>
</form>
</div>
and then Request it from Controller:
#RequestMapping(value = "/", method = RequestMethod.GET)
public String index(Model model) {
if (PortalSessionProvider.getUserSession().isAuthenticated()) {
LoadContext l = new LoadContext(User.class);
l.setQueryString("select u from sec$User u");
model.addAttribute("users", dataService.loadList(l));
}
return "index";
}
Im trying to create 2 instances of an object and then parse them to the controller via an input type="submit".
Here's what I have:
#RequestMapping (value = "/webdata")
public String vorsorgestammdaten(Model model) {
model.addAttribute("Data", new DataOp());
model.addAttribute("Data2", new DataOp());
return "/webdata";
}
// Here I want to parse both objects
#RequestMapping(value = "/formdata", method = RequestMethod.POST)
public String formdata(HttpServletRequest req, #AuthenticationPrincipal User currentUser,
DataOp Data, DataOp Data2, Model model) {
// Do stuff for example
Data.getName();
Data2.getName();
}
Here is part of my forms:
<form method="post" th:action="#{/formdata}" th:object="${regData}">
<div class="col-sm-4">
<input th:field="${Data2.gender}" type="radio" value="MALE" name="gender" disabled="disabled" /><span>Mr</span>
<input th:field="${Data2.gender}" type="radio" value="FEMALE" name="gender" disabled="disabled"/><span>Frau</span>
<p>Firstname:</p>
<input required="required" type="text" th:field="${Data2.firstname}" placeholder="Max" disabled="disabled" />
</div>
<div class="col-sm-4">
<input th:field="*{Data.gender}" type="radio" value="MALE" name="gender" disabled="disabled" /><span>Mr</span>
<input th:field="*{Data.gender}" type="radio" value="FEMALE" name="gender" disabled="disabled"/><span>Frau</span>
<p>Firstname:</p>
<input required="required" type="text" th:field="*{Data.firstname}" placeholder="Max" disabled="disabled" />
<button type="submit">Speichern</button>
</div>
</form>
Try removing this th:object="${regData}".
Then you can use #ModelAttribute annotation.
For example,
public String formdata(HttpServletRequest req,#AuthenticationPrincipal User currentUser, #ModelAttribute DataOp Data,#ModelAttribute DataOp Data2, Model model) {
//Some code
}
You might as well switch *{Data.gender} to ${Data.gender}.
add RequestAttribute annotation to each of those params, adding the "name" attribute to differentiate them.
here you have some info.
I have a Html page called kind.html inside the WEB-INF directory and an other jsp page called registration.jsp inside the WEB-INF folder. I need to put this registration.jsp page inside the WEB-INF directory so it cannot be accessible if a user attempts to get access to it by typing its URL. So my problem is how can i navigate from kind.html to registration.jsp with link called home I am newbie in this Thank you.
below is my code snippet and png file
kind.html..............................................
<li class='active'><a href='kind.html'><span>Home</span></a></li>
<li class='has-sub'><span>Register</span>
.............................registrationcontroller........................................
#RequestMapping(value="/registration",method = RequestMethod.POST)
public #ResponseBody
String firstRegistration(HttpServletRequest req,
HttpServletResponse response) {
response.setContentType("text/html");
RegistrationModel registrationModel = new RegistrationModel();
registrationModel.setFirstName(req.getParameter("first_name"));
System.out.println("controller " + req.getParameter("first_name") );
registrationModel.setLastName(req.getParameter("last_name"));
registrationModel.setPassword(req.getParameter("password"));
registrationModel.setEmailID(req.getParameter("email"));
System.out.println("controller email " + req.getParameter("email"));
SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
try {
Date date = format.parse(req.getParameter("BirthDate"));
registrationModel.setDOB(date);
} catch (Exception e) {
e.printStackTrace();
}
String phoneno=req.getParameter("phoneNo");
Integer phoneNo = Integer.parseInt(phoneno);
System.out.println("phone no ...."+phoneNo);
registrationModel.setPhoneNo(phoneNo);
registrationModel.setGender(req.getParameter("gender"));
String age=req.getParameter("Age");
Long AGE = Long.parseLong(age);
registrationModel.setAge(AGE);
registrationModel.setAvtar(req.getParameter("Avtar"));
System.out.println("avtar"+ req.getParameter("Avtar"));
Address address = new Address();
address.setAddressline(req.getParameter("Full-Address"));
address.setCity(req.getParameter("city"));
address.setLandmark(req.getParameter("landmark"));
address.setState(req.getParameter("state"));
String zipCode =req.getParameter("Zipcode");
Long zipcode = Long.parseLong(zipCode);
address.setZipcode(zipcode);
registrationModel.setAddress(address);
registrationService.resgistration(registrationModel);
return "registration.jsp";
}
..........................registration.jsp.........................................................
<form action="registration" method="post">
<fieldset>
<legend>Register Form</legend>
<div>
<input type="text" name="first_name" placeholder="First Name" />
</div>
<div>
<input type="text" name="last_name" placeholder="Last Name" />
</div>
<div>
<input type="password" name="password" placeholder="Password" />
</div>
<div>
<input type="text" name="email" placeholder="Email" />
</div>
<div>
<input type="text" name="BirthDate" placeholder="BirthDate" />
</div>
<div>
<input type="number" name="Age" placeholder="Age" />
</div>
<div>
<select name="gender">
<option value="select">i am..</option>
<option value="m">Male</option>
<option value="f">Female</option>
</select><br> <br>
</div>
<div>
<input type="number" name="phoneNo" placeholder="PhoneNo" />
</div>
<div>
<input type="text" name="Full-Address"
placeholder="Full-Address" />
</div>
<div>
<input type="text" name="landmark" placeholder="landmark" />
</div>
<div>
<input type="text" name="city" placeholder="city" />
</div>
<div>
<input type="text" name="state" placeholder="state" />
</div>
<div>
<input type="number" name="Zipcode" placeholder="Zipcode" />
</div>
<div>
<input type="file" name="Avtar" placeholder="Avtar" />
</div>
<input type="submit" name="submit" value="Send" />
</fieldset>
</form>![folder structure of project][2]
1st to call your pages html is static so you can use mvc resources "" for it, for jsp you have to use ViewResolver for jstl use InternalResourceViewResolver in its configuration as :
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/view/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
then in your controller use
return "registration";
also you will need method to handle GET request and change below line
<li class='has-sub'><span>Register</span>
if you are using Spring boot you will find these configuration automated.
So my Spring MVC project home page has two input fields within a form.
The user can enter the roll number or name.
Jsp
<form action="search" method="GET" >
<div style="text-align: center;">
<input type="text" id="regNo" name="regNo" size="30" maxLength="50" "></input> or
<input id="studentName" type="text" size="30" maxLength="50" "></input>
</form>
In my controller, I have two methods mapped to two input fields.
#RequestMapping(value ="/search", method = RequestMethod.GET)
public String getStudent(String regNo, ModelMap model){
return "A";
}
#RequestMapping(value="/search", method = RequestMethod.GET)
public String searchStudentByName(String studentName, ModelMap model){
return "B";
}
Since both the input fields are String, I don't know how can I map them to two different methods in controller?
Did you want something like this :
#RequestMapping(method= RequestMethod.GET,value = "/search/r/regNo={regNo}")
public String getStudent(#PathVariable String regNo){
}
for studentName :
#RequestMapping(method= RequestMethod.GET,value = "/search/s/studentName={studentName}")
public String getStudent(#PathVariable String studentName){
}
then you need to add both request in different form tag, & in action tag provide :
if regNo send :
/search/r/
for studentName :
/search/s/
Jsp :
<form action="search/r" method="GET" >
<div style="text-align: center;">
<input type="text" id="regNo" name="regNo" size="30" maxLength="50" "></input>
<input type="submit" name="approve" value="RegNo" />
</form>
<form action="search/s" method="GET" >
<div style="text-align: center;">
<input id="studentName" type="text" size="30" maxLength="50" "></input>
<input type="submit" name="approve" value="StudentName" />
</form>
OR Second way to do so :
<form action="search" method="GET" >
<input type="text" id="regNo" name="regNo" size="30" maxLength="50" "></input>
<input id="studentName" type="text" size="30" maxLength="50" "></input>
<input type="submit" name="regno" value="regno" />
<input type="submit" name="studentName" value="studentName" />
</form>
controller :
#RequestMapping(value = "/search", method = RequestMethod.GET, params = { "regno" })
public String getRegno(#RequestParam String regno) {
}
#RequestMapping(value = "/search", method = RequestMethod.GET, params = { "studentName" })
public String getStudent(#RequestParam String studentName) {
}
Post me.
So I am trying to pass values from my form to the controller. The controller then uses those values to create a "Course". I can't seem to pass the data but courses are being created.
Here is my view
#{form #Courses.save()}
<div>
Course Name: <input type="text" name="courseName" />
</div>
<div>
Course Code: <input type="text" name="courseCode" />
</div>
<div>
Course Description: <input type="text" name="courseDescription" />
</div>
<div>
Course Credit: <input type="text" name="courseCredit" />
</div>
<div>
Course Total Capacity: <input type="text" name="courseTotalCapacity" />
</div>
<div>
Course Is Active?: <input type="text" name="courseIsActive" />
</div>
<div>
Course Department: <input type="text" name="department" />
</div>
<div>
<input type="submit" value="Create Course" />
</div>
#{/form}
And here is my controller method
public static void save(String course_name, String course_code, String course_description, String credit, String capacity, String isActive){
Course course = new Course(course_name, course_code, course_description, credit, capacity, isActive).save();
course.save();
renderJSON(course);
}
Your input names in the HTML are not matching the actions method's parameters (Instead of course_name you need to write courseName and so on, like in the HTML).
public static void save(String courseName, String courseCode, String courseDescription, String courseCredit, String courseTotalCapacity, String courseIsActive){
Course course = new Course(courseName, courseCode, courseDescription, courseCredit, courseTotalCapacity, courseIsActive).save();
course.save();
renderJSON(course);
}
In your view you could replace your data name to course.Name, course.Code, ...
#{form #Courses.save()}
<input type="text" name="course.name" />
<input type="text" name="course.code" />
<input type="text" name="course.description" />
...
#{/}
And in your controller, you could write this:
public static void save(Course course){
course.save();
renderJSON(course);
}