我如何提交点击页面上放置的表单?

时间:2019-06-11 13:53:27

标签: html spring email controller thymeleaf

我是Spring和Thyme Leaf的新手,但我想点击提交表单。

我要搜索Contact.html页面,然后填写表单并单击带有“ / send”操作的按钮,然后提交表单。

@Controller
@RequestMapping("/Contact")
public class EmailSender {

@Autowired
private JavaMailSender sender;

@RequestMapping(value = "/send", method = RequestMethod.GET)
public String processMail() throws Exception {

    try {
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);

        helper.setTo("xx@gmail.com");
        helper.setText("xx");

        helper.setSubject("Hi!");

        sender.send(message);
        return "Contact";

    } catch (Exception ex) {
        return "Error in sending email: " + ex;
    }

}

因此,如果我这样做,将会出错。有人可以帮帮我吗?谢谢!

1 个答案:

答案 0 :(得分:0)

经过更多调试后,这是工作代码:

@Autowired
private JavaMailSender sender;

@GetMapping("/send")
@RequestMapping
public String sendForm() {
    return "Contact";
}

@PostMapping(value="/send")
public String processForm() throws Exception {

    try {
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);

        helper.setTo("xx@gmail.com");
        helper.setText("xx");

        helper.setSubject("xx");

        sender.send(message);
        return "Contact";

    } catch (Exception ex) {
        return "Error in sending email: " + ex;
    }

}

感谢所有的建议和帮助!