I am writing application on playframework. I have a search form and I want to send POST request and be able to accept it in my controller, how this is possible?
There Many samples for it, First Add a method login in your Application (or whatever) controller.
Lets assume your posting login details for your controller.
public static void login(String userCode,String password){
User loginUser = User.find("byUserCodeAndPassword",userCode,password).first();
if(loginUser == null){
flash.put("username",userCode);
flash.error("Invalid Credentials");
index();
}
else{
Cache.set(session.getId(),loginUser,"20mn");
Home.Home();
}
}
In your conf/routes file add
POST / Application.login
Assuming you have a index.html in your app/views/Application folder.
From the Html Page:
<div id="login">
#{form #login(), id:'formLogin'}
<p class="field">
<label>User Code:</label>
<input type="text" name="userCode" size="19" value="${flash.userCode}" required>
</p>
<p class="field">
<label>Password:</label>
<input type="password" name="password" size="19" required>
</p>
<p class="buttons">
<input type="submit" value="Login">
</p>
#{/form}
Related
I'm using thymeleaf for my web application. There is a problem about save and update functions. When I want to save a campaign from ui, the campaign object fields come null to rest controller class. This problem just occure in weblogic server(12.1.3). When I try it in tomcat server, do not occure any error.
my edit and create page as follow. There are a few fields for campaign, but I wrote some of them in here. Bytheway, I am sure that all fields ready in the html page. Some of them hidden, some of them visible.
<div class="row">
<form name="Form" class="col s8 offset-s2" id="upload-file-form"
enctype="multipart/form-data" th:object="${campaign}"
th:action="#{/admin/getCampaign}" onsubmit="return validateForm()"
method="post">
<div class="row">
<input type="hidden" th:field="*{id}"/>
<input type="hidden" th:field="*{version}"/>
</div>
<div class="row">
<div class="input-field">
<input id="brandname" type="text" class="validate" th:field="*{brandname}">
<label for="brandname">Brand Name</label>
</div>
</div>
</form>
</div>
#RequestMapping(value = "admin/getCampaign", method = RequestMethod.POST)
public String uploadingPost(#RequestParam("uploadingFiles") MultipartFile[] uploadingFiles,
#RequestParam("uploadingFiles1") MultipartFile[] uploadingFiles1,
#RequestParam("uploadingFiles2") MultipartFile[] uploadingFiles2,
#RequestParam("uploadingFiles3") MultipartFile[] uploadingFiles3,
Campaign campaign) throws IOException {
/** this is my controller method for save or update.
*/
}
in weblogic server campaign parameter fields come null (as a new object), but in tomcat server, everything is normal.
UPDATE:
I changed my ui fields to value like this post. But the problem continue.
<input type="hidden" th:value="*{id}"/>
Your form enctype is "multipart/form-data".
So you must add spring.http.encoding.enabled=false to application.properties.
It should be <input type="hidden" th:field="*{id}"/>not th:value
I have a question to you. Let me explain the situation. We have a jsp page, there is a post form here with some inputs. When user enter some data and submit the form, my spring controller handles this request, transforms income data and then I should send it's trasformed data with post request to another site with the client redirection. In other words I want to know is it some oppotunity to use response.sendRedirect("some other website url") with post request in my controller? Or how I can do it another way?
For example simple form:
<form action="myServerSideUrl" method="post>
<input type="text" name="Input1"/>
<input type="text" name="Input1"/>
<input type="submit" value="submit"/>
</form>
When user submit it, it comes to server, then for example I sum Input1 and Input2 (Input3), and then I want to redirect user to another site with Input3 POST request.
You may use Spring's own RestTemplate or a simple java.net.URLConnection (but you have to write a bit more code) to make a POST request in your controller.
So upon receiving the request in your #Controller you can just forward it wherever you want.
<html:form action="changePassword" method="POST"
commandName="changePasswordForm">
<label> <html:password path="password" id="oldPassword"
maxlength="128" class="input" required="required"
placeholder="Current Password" />
</label>
<label> <html:password path="newPassword" id="password"
maxlength="15" required="required" class="input"
placeholder="New Password" />
</label>
<label> <html:password path="confirmPassword"
id="confirmNewPassword" maxlength="15" required="required"
class="input" placeholder="Confirm Password" />
</label>
<label> <html:input path="" type="submit" value="SAVE"
/>
</label>
</html:form>
Spring Controller is below :
#RequestMapping(value = "changePassword", method = RequestMethod.POST)
public String changePassword( #ModelAttribute(value = "searchForm") SearchForm searchForm,
Model inModel,
HttpServletRequest inRequest )
{
String viewName ="changePassPage";
// do something logic related to change password.
return viewName ;
}
Form Bean Or POJO Model in spring:
public class SearchForm
{
private String password;
private String newPassword;
private String confirmPassword;
//setter & getter;
}
You can try with Spring Post Example
im doing a simple login page:
<div class="form-signin" role="form" ng-controller="loginController">
<div class="center-block" ng-repeat="erro in erros">
<span class="error">{{erro.message}}</span>
</div>
<h2 class="form-signin-heading">Identifique-se</h2>
<input type="email" class="form-control" placeholder="Endereço de Email" ng-model="usuario.email" autofocus>
<input type="password" class="form-control" placeholder="Senha" ng-model="usuario.senha" required>
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me"> Lembrar Dados
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" ng-click="Logar()" type="submit">Entrar</button>
</div>
it do a POST to "/MyService/Example/Login" javaee backend using the usuario.email and usuario.senha fields, i can read and return true or not normally, but my doubt is, how can i implement a Token? im already generating a token using the UUID random + useremail + creation time. I have to send it with the JSON? i have to do a request.addHeader in the backend and add the token there? and how can i implement a kind of service that EVERY page it do a "requestLogin", so when users login for first time it send a blank token-cookie, after login server return the cookie, user store in the cookie, and for every page/request it send the token-cookie back and i can verify if its authentic, if not, send false and than angular return to the login page?!
i mean, im not asking for code, i saw some tutorials about token + angular, but was "simple" to understand explaining why and how to use the thing =\
I am new to JSP/Servlet.
I want to use one servlet for handling different request sent from different JSP form/button.
For example
student.jsp =>
<form action="SingleController?action=student" method="POST">
Student ID: <input type="text" name="stid">
Name: <input type="text" name="stname">
<input type="submit" value="Submit" name="student">
</form>
teacher.jsp =>
<form action="SingleController?action=teacher" method="POST">
Teacher ID:<input type="text" name="tid">
Name: <input type="text" name="tname">
<input type="submit" value="Submit">
</form>
SingleController
How can I manage these two different requests using a single Servlet ?
use if conditions in servlet on action .
String action = request.getParameter("action");
if (action.equals("student")){
// to do rest of code..
}
else if (action.equals("teacher")){
// to do rest of code..
}
i have created a java project in eclipse,in this project i have an html form customer.html as shown below
<form>
Name :<input type="text" id="name"/>
Address: <input type="text" id="add"/>
Age:<input type="int" id="age"/>
<input type="submit" onclick="what should i call here in my program"/>
</form>
once the user fills the form and submits it i want to access the form field in java code is it possible.if so how or else is there anyother solution to create an xml file for this form
<input type="submit" onclick="only Javascript function can be called here"/>
if you want to access html form elements at java code you need to submit your form at some controller or another jsp. Like..
<form name="new_employee" method="post" action="../admin/newEmployee.do">
Name :<input type="text" name="employeeName"/>
Address: <input type="text" name="add"/>
Age:<input type="text" name="age"/>
<input type="submit" value="Submit"/>
</form>
form elements can be accessed from generated request by their names like..
request.getParameter("employeeName");