Open Dr4K4n opened 3 years ago
@sl1nkZz
Hey Stefan, im stuck. I created a Web Controller but i don't know how to parse the information from my actual contact.php to it. Maybe you can help me out?
@Dr4K4n
Thanks for your question :-) I think you mean contact.html not .php right? When you post from the contact.html to the Web Controller you can simply add method parameters inside the controller that have the same name as the field you post. So if you put "String country" there, you should receive the country from your form. Does that help?
i barely understand it yes, but i need to lern how tto work with Java so i think this task will take some time
Do i need to add a new class for every String given from contact.html?
So acutally my code looks like this
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
class ContactFormData {
@PostMapping("/contact.html")
public String firstName(@RequestParam(name = "firstName", required = true, defaultValue="Empty") String firstName, Model model) {
model.addAttribute("firstName", firstName);
return firstName;
}
@PostMapping("/contact.html")
public String lastName(@RequestParam(name = "lastName", required = true, defaultValue = "Empty") String lastName, Model model) {
model.addAttribute("lastName", lastName);
return lastName;
}
@PostMapping("/contact.html")
public String country(@RequestParam(name = "country", required = true, defaultValue = "Empty") String country, Model model) {
model.addAttribute("country", country);
return country;
}
@PostMapping("/contact.html")
public String subject(@RequestParam(name = "subject", required = true, defaultValue = "Empty") String subject, Model model) {
model.addAttribute("subject", subject);
return subject;
}
}
@Dr4K4n
But now i don't know how to output the data fetched from "contact.html" from the Java Class
Okay, your controller should have 2 methods.
One @GetMapping
which basically returns the contact.html.
It will be nearly identical to the example from here: https://spring.io/guides/gs/serving-web-content/
/contact should be the URL: @GetMapping("/contact")
Then you should put your contact.html inside src/main/resources/templates/
because it will serve as a thymeleaf template
Inside your @GetMapping
method you simply return "contact"
. This will cause Spring to search for a template with the filename contact.html
inside src/main/resources/templates/
and use it as the template to render the response HTML to the client.
Afterwards, if you got to http://localhost:8080/contact you should see your contact.html page, returned from the server 😃
If you have done this, I'll tell you about the second method 😉 the @PostMapping
for the data posted from the form.
To answer your question, you need only one @PostMapping
method to handle the Strings (firstName, country, etc.) posted from the contact.html
okay i've tried to get it to work but i dont know why the page isn't loading.
This is my code now:
package com.demo.servingwebcontent;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
class GreetingController {
@GetMapping("/contact")
public String contact(@RequestParam(name="contact", required=false, defaultValue="World") String contact, Model model) {
model.addAttribute("contact", contact);
return "contact";
}
}
That looks fine! can you push the code? Then I can take a closer look. Maybe the contact.html file is in the wrong place
I failed to push the files corretly but here is the code https://github.com/sl1nkZz/assessment-2021-06/commit/c501d3181e961fd71f59a874eba415f67abfa215
The contact.html is in the right place. But I'm missing an application.class. it should have been generated by start.spring.io, did you delete it? How do you start the application in intellij then?
No i didn't deleted anything. I'm starting the application in my browser via http://localhost/contact. How can i run the application in IntelliIJ?
Sorry for the late reply. One but difference between the PHP and the Java/Spring ecosystem is how you actually run the application. With PHP you put your files on a Webserver, usually running Apache with PHP extension. The Webserver runs on its own and you simply update the files and you can see the results instantly by reloading the page. With spring you are building a Java application, that runs on its own and serves http content on port 8080. With each change you make to the code you have to recompile and re-run the application (there are tools to make this easier). Intellij makes building and running the application a little bit more comfortable. In general you need a class with a main() method, which is used as main entry point into the application. The spring initializr should have generated one for you, looking similar to this code. If you open it in intellij it should display a little green arrow next to the line numbers, use that one to start the application. Then go to http://localhost:8080/contact to talk to your controller.
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Okay when i want to start the app via IntelliJ i need to configure app settings, what should i enter there?
Hmm, there should be no need to configure anything. Did you import the project into IntelliJ as maven project? File -> Open -> Navigate to the project directory and choose the pom.xml file
yea i think i did, maybe i did something wrong? idk,
can you provide a screenshot maybe?
At the right you can see Maven written in IntelliJ
Ah, I see a problem. You wrote the code for the ContentController inside the DemoApplication.java file! You need to separate files, one DemoApplicaiton.java with the content posted by me above and one ContentController.java with the content from your screenshot. Then you should be able to start the application from inside the DemoApplication class (there should be a green arrow button next to the line numbers).
Okay i've changed it to this
I think i need to call the ContentController-Method in the DemoApplication.java file, but I don't know how
Nice, this looks correct! No, nothing like that needed. Spring will scan all classes on startup that are annotated with @Controller and take care of the rest. You should be able to start your application, see the green arrows in your screenshot, next to the line numbers and then navigate to http://localhost:8080/contact
if i start the application it says
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.1)
2021-06-24 14:54:43.950 INFO 6344 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication using Java 16.0.1 on Dome-PC with PID 6344 (C:\Users\Domi\Desktop\demo\target\classes started by Domi in C:\Users\Domi\Desktop\demo)
2021-06-24 14:54:43.953 INFO 6344 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2021-06-24 14:54:44.942 INFO 6344 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.546 seconds (JVM running for 2.068)
Process finished with exit code 0
If i try to open localhost:8080/contact then i get a network timeout
It should not say "Process finished with exit code 0"... that means the server stopped, it should stay running
Well i don't know why it says "Process finished with exit code 0" it's a clean maven project with thymeleaf added
Hm, please push the code. Then I can take a look at the pom.xml. Maybe Spring Web is missing as dependency?
pushed it here https://github.com/sl1nkZz/assessment-2021-06/tree/main/demo
I found 2 small problems in your code and fixed and pushed them. You can pull the updates and it should run on your machine too ;-)
Ah now it's working
Ok, cool that is also works on your machine now. I'm on vacation now, I will return July 12th, should we have another (video-)chat then?
Sounds great, wish you a lot of fun on vacation
PHP is your current backend. Let's replace it with Java and Spring Boot 😃 This should really take you out of your comfort zone and I expect some questions 😉
You need a current JDK on your machine and an IDE (we recommend IntelliJ IDEA). A good starting point then is https://start.spring.io There you can get a basic project setup that you can then import into IntelliJ.
To replace your contact.php with Spring Boot code, you will need a controller (Java Code to handle the POST request) and a HTML-template, to render the result to the client. We use Thymeleaf as our templating engine, you'll need to add that as dependency on https://start.spring.io A simple tutorial is this one: https://spring.io/guides/gs/serving-web-content/ (you should be able to skip the setup part and directly go to "Create a Web Controller")
Good luck 🤞🏼