I might be asking a really dumb question here. I am trying to learn Spring boot and I have created a rest controller, and with a simple HTML I am able to do post and get.
As I am mainly coding in Java, I do not have much knowledge on frontend development like php/html/javascript etc. Is it possible to test and develop codes using JFrames and JButtons etc?
The sample code below shows that when I run a JFrame, when clicking on the JButton, I wish to send the inputs in the textfield to my "PersonController".
Person Class:
@Entity
public class Person {
String lastName;
String firstName;
public Person(String lastName, String firstName) {
super();
this.lastName = lastName;
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
Rest Controller:
@RestController
public class PersonController {
private final PersonService personService;
@Autowired
public PersonController(PersonService service) {
this.personService = service;
}
// ------- THIS IS THE METHOD I WANT TO CALL WHEN THE BUTTON IS CLICKED
@PostMapping("/add")
public void addPerson(@RequestBody Person person) {
this.personService.handleNewPerson(person);
}
}
UI Using Java:
public class MainClass {
public static void main(String[] args) {
JFrame frame = new JFrame("Test Frame");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
JLabel lastNameLabel = new JLabel("Last name: ");
JTextField lastNameTextfield = new JTextField(50);
JLabel firstNameLabel = new JLabel("First name: ");
JTextField firstNameTextfield = new JTextField(50);
JPanel firstNamePanel = getInputPanel(firstNameLabel, firstNameTextfield);
JPanel lastNamePanel = getInputPanel(lastNameLabel, lastNameTextfield);
JButton button = new JButton("Click me");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// ------ Pack and send firstname/lastname to my rest controller
}
});
p.add(lastNamePanel);
p.add(firstNamePanel);
p.add(button);
frame.add(p);
frame.pack();
frame.setVisible(true);
}
private static JPanel getInputPanel(JLabel label, JTextField tf) {
JPanel panel = new JPanel(new FlowLayout());
label.setPreferredSize(new Dimension(100, label.getPreferredSize().height));
panel.add(label);
panel.add(tf);
return panel;
}
}
Is this even possible? Or in order for me to properly learn how to code using Spring boot, or to develop my own simple project, I'd also have to learn how to create webpages etc?
------------------------------------------ Edit: ----------------------------------------------
To further explain why I am doing this, one example is when I have a simple HTML(index.html), and do a post with user's first/last name input, I am unable to parse first/last name into "Person" json without the use of javascript(which I currently do not have knowledge on). Example below:
index.html
<form action="add" method="POST">
<div><label>Last Name:</label> <input name="lastName" type="text" value="" /></div>
<div><label>First Name:</label> <input name="firstName" type="text" value="" /></div>
<button>Add Manga</button>
</form>
If my method takes in params as first and last name it would work:
@PostMapping("/add")
public void addManga(@RequestParam(name = "lastName") String lastName, @RequestParam(name = "firstName") String firstName) {
Student student = new Student(lastName, firstName);
this.personService.handleNewPerson(person);
}
But if my method takes in the Person object, the form post wouldn't work unless I parse it into a json?
@PostMapping("/add")
public void addPerson(@RequestBody Person person) {
this.personService.handleNewPerson(person);
}