My problem is that the submit-button below the form should redirect the registrating user to the welcome page after pressing it.
So far, the button wants to redirect to "welcome.html" but I constantly get an error 404, which I don't understand.
The welcome-html doesn't do anything fancy, just a simple text saying hello.
My Controller looks as follows:
package user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
#Controller
public class UserController {
#Autowired private UserRepository users;
#Autowired private UserManagement UserManager;
#GetMapping("/")
public String register(Model model, RegistrationForm form) {
System.out.println("Homepage");
model.addAttribute("form", form);
return "register";
}
#PostMapping("/register")
public String submit(#ModelAttribute("user")User user, RegistrationForm form,
Model model) {
User newUser = UserManager.createUser(form);
model.addAttribute("name", newUser.getAccount().getUsername());
model.addAttribute("street", newUser.getStreet());
model.addAttribute("PLZ", newUser.getPLZ());
model.addAttribute("mail", newUser.getMail());
model.addAttribute("number", newUser.getPhoneNumber());
users.save(newUser);
return "redirect:/welcome";
}
}
`
the register-page functions alright and the button too:
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" type="text/css" th:href="#{/resources/css/style.css}" href="../static/resources/css/style.css"/>
<title>register</title>
</head>
<body>
<header id ="mainHeader">
<div class ="container">
<h1>ClownCollege</h1>
</div>
</header>
<nav id="mainNav">
<div class="container">
<ul>
<li>Startseite
<li>Angebote
<li>Login
</ul>
</div>
</nav>
<div id="register">
<h2>Registrierung</h2>
<form modelAttribute = "form" method ="post" th:object=${form}>
<input type="text" field ="*{name}" />
<input type="text" field ="*{street}"/>
<input type="text" field ="*{PLZ}"/>
<input type="text" field ="*{mail}"/>
<input type="text" field ="*{number}"/>
<input type="text" field ="*{password}"/>
</form>
<button type ="submit">registrieren</button>
</div>
</body>
</html>
[source folders for path finding]
Please add action="register" on your <form>. It is specifying where does the form go to after submitting.
<form action="register" modelAttribute = "form" method ="post" th:object=${form}>
Remove <a href ="welcome.html">. Because your controller has redirect statement return "redirect:/welcome";, a html link is unnecessary.
It should be work as you expect.
Related
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Page</title>
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
</head>
<body>
<div class = "tela">
<h1>Login</h1>
<form action="/" method = "post">
<input class = "inf" type="text" placeholder="Email">
<br><br>
<input class = "inf" type="password" placeholder="Senha">
<br><br>
<button class = "main-btt">Log in</button>
</form>
<form action="/criarconta" method="post">
<button class = "criar">Criar conta</button>
</form>
</div>
</body>
</html>
from flask import Flask, request, render_template, redirect
app = Flask("__name__")
#app.route("/")
def index():
return render_template("login.html")
#app.route("/criarconta", methods=["POST"])
def create():
return render_template("criarconta.html")
I'm working on a Login page, with flask, html and css, but always that I run flask the link that shows up is a html page without css, even when I link css in the html file. I want to resolve tis, and finds out how to open the page and the css is there. OBS: When I open the file alone, without flask, it works.
I tried using href="styles.css" but it didn't work so I changed for url_for, but its still not working.
I think the problem is in the html but I putted the python if you need.
Good Evening everyOne, i'm trying to make a web app using Spring boot and Thymeleaf. my problem is that all the redirect links in my index html file send me to one link which is my Login.html file.
this is my cotroller :
#Controller
public class HomeController {
#RequestMapping("/")
public String index() {
return "index";
}
#RequestMapping("/login")
public String login() {
return "login";
}
#RequestMapping("/signuo")
public String signup() {
return "signup";
}
}
Login and SignUp page. (they are not complete, its just a test)
<!DOCTYPE html>
<html xmlns:th = "http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>SignUp</title>
</head>
<body>
<h1>This is the SignUp page</h1>
</body>
</html>
<!DOCTYPE html>
<html xmlns:th = "http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>This is the Login page</h1>
</body>
</html>
the index page is so long : i'm gonna give you the important parts
"Stuuuf here ... "
<a th:href="#{/index.html}" class="nav-brand"><img src="img/core-img/agri_en_logo.png" alt="" /></a>
.....
<ul class="dropdown">
<li><a th:href="#{/index}">Acceuil</a></li>
<li>Onca</li>
<li>ONICL</li>
<li>IAV</li>
</ul>
<ul><li><a th:href="#{/login}">S'inscrire</a></li></ul>
................
<div class="header-add-area">
<a th:href = "#{/signup}">
<button type="submit" class="btn newsbox-btn 100">S'authentifier</button>
</a>
this is the hierarchy of my project :
hierarchy of my project
if you need something else add it in comments, if you need also my hole project i can send it to you in private.
Thanks in advance
I'm getting the below error; I tried everything but not getting a resolution:
HTTP Status 415 – Unsupported Media Type
I am trying to send JSON object to the controller as put it in the #Requestbody
<%#page import="org.json.JSONObject"%>
<%# page language="java" contentType="application/json charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Welcome</h1>
Register
Delete
<%
JSONObject obj = new JSONObject();
obj.put("id", "1");
obj.put("name", "ABC");
%>
<form action="./jsonreq" method="post">
<input type="hidden" name="obj" id="obj" value="<%=obj%>">
<input type="submit" value="Submit"/>
</form>
</body>
</html>
2)Controller:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.springhibernate.bean.Employee;
import com.springhibernate.service.EmployeeService;
#Controller
public class RegistrationController {
#RequestMapping(path="/jsonreq", method=RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE)
public #ResponseBody Employee json(#RequestBody Employee obj)
{
System.out.println("JSON"+obj.toString());
return obj;
}
}
You're getting a 415 status because your browser is sending a request with a content type of application/x-www-form-urlencoded to an controller method which only accepts application/json.
Ask yourself if you really need to send JSON data in this way instead of as part of the form.
If you do, one way of accomplishing it is to use Javascript to compile the data from the form (or elsewhere) as JSON and make an XMLHttpRequest posting it to your server.
Another much less ideal way would be to remove your consumes constraint on the controller method, change the argument to #RequestParam("obj") String obj and parse the response in the controller method manually using an Autowired ObjectMapper using objectMapper.readValue(obj, Employee.class).
I have a Django model/view/form that is rendering correctly in the template, but it is not submitting the data that is input to the database. Any help with this would be greatly appreciated!
#models.py
from django.db import models
from django.forms import ModelForm
class UserRegistration(models.Model):
user_first = models.CharField(max_length=50)
user_last = models.CharField(max_length=50)
user_email = models.EmailField()
#user_fantasyhost = models.CharField(max_length=50)
def __unicode__(self):
return u'%s %s %s' % (self.user_first, self.user_last, self.user_email)
class RegForm(ModelForm):
class Meta:
model = UserRegistration
#views.py
from django.shortcuts import render_to_response
from django.shortcuts import render
from django.http import HttpResponse, HttpRequest, HttpResponseRedirect
from acme.dc_django.models import UserRegistration
from acme.dc_django.models import RegForm
def regPage(request, id=None):
form = RegForm(request.POST or None,
instance=id and UserRegistration.objects.get(id=id))
if request.method == 'POST' and form.is_valid():
form.save()
return HttpResponseRedirect('/league_setup/')
user_info = UserRegistration.objects.all()
context = {
'form':form,
'user_info' :user_info,
}
return render(request, 'regpage.html', context)
#repage.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<HTML lang="en">
<head>
<title>User Registration</title>
</head>
<body>
<form method="POST" action="/league/">
{% csrf_token %}
<table>{{ form }}</table>
<input type="submit" value="Create Account"
</form><br /><br />
</body>
</HTML>
Thank you for your help,
dp
I tried your code. Your problem is that the action attribute of your html form tag is set to "/league/".
Unless reqPage url is actually "/league/", it won't work. When i changed action="/league/" to action="" as such:
<HTML lang="en">
<head>
<title>User Registration</title>
</head>
<body>
<form method="POST" action="">
{% csrf_token %}
<table>{{ form }}</table>
<input type="submit" value="Create Account" />
</form><br /><br />
</body>
</HTML>
The form did work:
In [3]: UserRegistration.objects.all()
Out[3]: [<UserRegistration: aoeu oeu oeu#aeou.com>]
If we write something as follow:
Link
It will call GET method of that servlet. Can I send post request on click of a tag? Is it possible?
I know how to do this with Javascript but want to know if this could be done without JavaScript.
The solution is to surround the anchor in a form, which has the post method and the action you wish to execute. On the anchor put a javascript to submit the form
<form name="submitForm" method="POST" action="/servlet/ServletName">
<input type="hidden" name="param1" value="param1Value">
Click Me
</form>
edit
I think I should mention that this isn't a good idea.
Links take you to pages, that's what users understand them to do. To break the users assumptions and cause a link to POST, to do an irrevocable thing, is generally considered a bad idea.
Use a button, label it semantically, then your user knows that clicking this does something.
second edit
I really need to emphasise that this isn't a good idea at all.
This breaks the internet.
Only with javascript: create a <form action="MyServlet"> and submit it with form.submit()
You can also send POST with ajax (with jQuery: $.post("MyServlet", {param:param}))
But think about the semantics. With POST you should post data. And links are usually simply getting resources. (It's another story if your link is actually a button in disguise)
Code for Login.jsp page:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
</head>
<body>
<form action="LoginServlet" method="post" name="credential">
Please enter userName :
<input type="text" name="un"><br>
Please enter PassWord :
<input type="text" name="pw"><br>
<input type="submit" value="Submit">
</form>
<form action="registerUser" name="registerUserForm" method="post">
If no user name and password then get a new one by clicking here
</form>
</body>
</html>
code for registerUser servlet::
package examplePackage;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet("/registerUser")
public class registerUser extends HttpServlet {
private static final long serialVersionUID = 1L;
public registerUser() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("registerUser");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}