Url with paramter as Button (Thymeleaf + Spring boot) - java

I'm using Spring Boot and Thymeleaf. A button should display the user another page if he clicks on the Button. The only problem I have is, the url needs a parameter.
I tried this, but this doesn't work:
<form th:action="#{/removeserver(id=${serverId})}">
<input type="submit" value="Remove server"/>
</form>
It works with the following link, but not as button:
<a th:text="Remove" th:href="#{/removeserver(id=${serverId})}"></a>
So how can I display a url with a parameter as a button?

I had similar problem so instead of using path-variable I used request-param:
<form action="archive?id=${x.id}" method="post">
<button type="submit">Archive</button>
</form>
You can try using this.

Related

JSP output on HTML site (without a changeover)

Whenever I click on a button in my HTML side, I'm switching to my JSP-File (localhost:8080/index.hmtl -> localhost:8080/result.jsp). But my intention is to load the JSP script in the background and put the result of the jsp on my HTML side so i still stay on localhost:8080/index.html. Summarized this is a quick overview of my HTML side:
<form action="result.jsp">
Some Input textfield: <input type="text" name="name">
<input type="submit" name="button" value="eintragen">
</form>
So, is there a special command for this or a easy way to solve this problem?

Playframework dynamic form handling

Hallo guys im new to the playframework and run into a little problem
regarding form handling.
Here is my view
<form action="#routes.Account.changeemail()" method="Post">
email:<input name ="email">
<button type="submit" name="action" value="Change_email">save</button>
<br />
</form>
<form action="#routes.Account.changepassword()" method="Post">
password:<input name ="password">
<button type="submit" name="action" value="change_password">save</button>
</form>
<br />
And here is my controller
public static Result changeemail(){
final DynamicForm form = Form.form().bindFromRequest();
Logger.info(form.get("email"));
return TODO;}
public static Result changepassword(){
final DynamicForm forms = Form.form().bindFromRequest();
Logger.info(forms.get("password"));
return TODO;}
Here the routes:
GET /account controllers.Account.accountview()
POST /account controllers.Account.changeemail()
POST /account controllers.Account.changepassword()
The problem is if i press the Change_email button it does the right thing, but if i press the password button it is doing the changeemail action , even if it should handle the changepasswort action. I checked it with the firefox networkanalysis and it seems that it is sending the correct action.
In forward thanks for the help
Greetings Alex
The problem comes from your routes, the order is important. Your router always takes the first POST /account which executes the changeemail() action. You can't have POST /account for two different actions. It should be :
GET /account controllers.Account.accountview()
POST /account/change-email controllers.Account.changeemail()
POST /account/change-password controllers.Account.changepassword()

It is possible to submit a hidden input by clicking on <spring:message>?

I thinked somthing like this, but I dont want to use a button to submit, I would submit by clicking on spring message. Is this possible somehow?
<spring:url value="/admin/messages" var="messagesUrl" htmlEscape="true"/>
<form action="${messagesUrl}" method="POST" class="new-message">
<input type="hidden" name="messageFromDashboard" value="true">
<spring:message code="${newMessage}">
<input type="submit" value="submit"></spring:message>
</form>
Maybe this will be helpful. Why you don't want to use submit button? If you just wanted to look it like a label/link just use css similar to http://jsfiddle.net/adardesign/5vHGc/
Html:
<button> your button that looks like a link</button>
Css:
button {
background:none!important;
border:none;
padding:0!important;
/*optional*/
font-family:arial,sans-serif; /*input has OS specific font-family*/
color:#069;
text-decoration:underline;
cursor:pointer;
}

How to Automate Web Login in Website abelhas.pt using Java

I am having a hard time trying to figure out the correct process for automating login for the following website: http://abelhas.pt
I have used Firefox's HttpFox plugin to discover the login form and action page url, in the generated HTML code and have found out that to perform login, I will need to post the following parameters:
Login=theLogin
Password=thePassword
Redirect=True
RedirectUrl=
RememberMe=false
FileId=0
The url to which the parameters are being passed when the user presses the submit button, is the following: "/action/login/login" (which is the one shown on the "action" attribute of the html element)
However, when generating and posting it using the following URL:
http://abelhas.pt/action/login/login?Login=theLogin&Password=thePassword&Redirect=True&RedirectUrl=&RememberMe=false&FileId=0
(Please note that the the values of "theLogin" and "thePassword" should be replaced by a valid login and password)
I am always getting "http://abelhas.pt/Error404.aspx", meaning that the following webpage was not found...
In the past, I have already been able to successfully automate logging-in in websites such as Google, filefactory, zippyshare, Imageshack, etc..however this one seems to be playing tricks on me...
Can someone help me understanding what is happening or what am I doing wrong?
Many thanks guys!
Gizmo
In my vaadin application i want to redirect to a website with automatic log in.
Now the issue is am unable to logIn through my java code which refers from
http://www.mkyong.com/java/how-to-automate-login-a-website-java-example/
Am also getting response code: 200, and unable to make use of response.
Following is the code snippet of the website to which i want to automate login
<form action="login.php" method="post" id="clientLogin">
<input type="hidden" name="__CSRFToken__" value="eac035fe0a1a64a9945e0ce798a44a52ca040b5c" /><div style="display:table-row">
<div class="login-box">
<strong></strong>
<div>
<input id="username" placeholder="Email or Username" type="text" name="luser" size="30" value="">
</div>
<div>
<input id="passwd" placeholder="Password" type="password" name="lpasswd" size="30" value=""></td>
</div>
<p>
<input class="btn" type="submit" value="Sign In">
</p>
</div>
<div style="display:table-cell;padding: 15px;vertical-align:top">
<div>
<!--<b>I'm an agent</b> —
sign in here
-->
</div>
</div>
</div>
</form>

go to jsp page by hitting back button

I have 2 jsp pages and one Servlet. I am fetching data from database by servlet and sending result to result.jsp page where i am displaying result. But i want to add a Back button in result.jsp , by clicking back button i want to go to index.jsp, but problem is when i am clicking back button everytime a message is coming Confirm form submission and it is irritating me. How can i avoid this Confirm form submission? perhaps it is coming as processing is done in servlet.
index.jsp
<form method="post" action="Student">
<input type="text" name="studentname"/>
<input type="submit" value="search"/>
</form>
Student servlet
String student_name=request.getParameter("studentname");
-------fetching data from database------------
-------------------sending to result.jsp------
String nextJSP = "/result.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(request,response);
result.jsp
//displaying data here
<form action="index.jsp">
<input type="submit" value="back"/>// a back button is here which is going to index.jsp
</form>
EDIT
From result.jsp i am going to another page result1.jsp like below
In result.jsp i have written the following:
<%out.println(student_name);%>
By clicking the above hyperlink i went to result1.jsp
I want to add a back button here(in result1.jsp) and after clicking i want to do to result.jsp, but when clicking back button i am getting Confirm form submission every time. I have written the following in result1.jsp
<input type="button" value="Back" onclick="javascript:history.go(-1)">
Still i am getting that message Confirm form submission. How can i avoid this? I want to go to result.jsp directly with out this message. How is it possible?
you can also use this to go one page back
<button type="button" name="back" onclick="history.back()">back</button>
You can write the below code that let's you to go index.jsp on your result.jsp page
Back
Try this
<button type="button"
name="back"
onclick='window.location='<your_path>/index.jsp'>back</button>
If you want a back button to go index.jsp, why not just make a normal link?
Back
There is only two way to get rid of the message, normal link or window.location. If the previous page is always the same, you don't need to use complicated function client side. If the page could be different, just post the link to result.jsp and use it to actually print a back link!
EDIT :
previous.jsp
<form method="post" action="Student">
<input type="hidden" name="back" value="previous.jsp" />
<input type="text" name="studentname"/>
<input type="submit" value="search"/>
</form>
result.jsp
out.println("Back");
This is the easiest way to create a goBack button using the method goBack(). This is the same as clicking the "Back button" used on the top of your browser.
<!DOCTYPE html>
<html>
<head>
<script>
/* The back() method loads the previous URL in the history list.
This is the same as clicking the "Back button" in your browser.
*/
function goBack() {
window.history.back()
}
</script>
</head>
<body>
<button onclick="goBack()">Go Back</button>
<p>Notice that clicking on the Back button here will not result in any action, because there is no previous URL in the history list.</p>
</body>
</html>
You can use a common button.
<input type="button" value="Back" onclick="javascript:history.go(-1)">
With history.go(-1), your browser will simply display previous page by reading from cache, it will not resubmit your data to the server, thus, no Confirm form submission will happen.
EDIT
I was wrong!
All you need is a client side redirect, you may write something like this in your result.jsp after it handles the form submitted from index.jsp:
response.sendRedirect("result.jsp");
So you will need a parameter to help you in result.jsp to tell whether it is a simple display request or a form submission, something like this:
String action = request.getParameter(action);
if("submit".equals(action)) {
// handle form data
response.sendRedirect("result.jsp");
} else {
// other code to display content of result.
}
In your index.jsp, it would be something like this:
....
<form action="result.jsp?action=submit" ...>
I experienced that form submit confirmation with Safari (not with IE/Chrome/FF).
The work around is submitting your form with "get" method. Of course this is only valid for "small" forms (max 2048 K).
Try this
<a href="${pageContext.request.contextPath}/index.jsp" class="btn btn-success">
Back
</a>
with bootstrap.css from here

Categories