0

what is expected

after data binding, web page should reflect the data

details

Controller code

@Controller
@Configuration
@Component
public class Controller {

    @Autowired
    Credentials c;

    @GetMapping("/greeting")
    public String greetingForm(Model model) {
        model.addAttribute("greeting", new RequestData());
        return "greeting";
    }

    @PostMapping("/greeting")

    public String greetingSubmit(@ModelAttribute RequestData greeting) {
        if (c.getUserName().equals(greeting.getId()) && c.getPassword().equals(greeting.getContent()))
        {
        return "result";
        }
        else
        {
            return "error";
        }
    }
}

RequestData.java

public class RequestData {

    private String id;
    private String content;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

}

Credentials.java

@Component
@Configuration
@PropertySource("classpath:application.properties")
public class Credentials {

    public String userName;
    public String password;

     @Autowired
     private Environment env;

    @Autowired
    public String getUserName() {

         return env.getProperty("spring.username");

            }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Autowired
    public String getPassword() {
         return env.getProperty("spring.password");

            }

    public void setPassword(String password) {
        this.password = password;
    }


}

result.html

 <!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head> 
    <title>Hello World</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Hello World</h1>
    <p th:text="'id: ' + ${greeting.id}" />
    <p th:text="'content: ' + ${greeting.content}" />
    <a href="/greeting">Submit another message</a>
</body>
</html>

able to compile, package the codebase and build the jar. however, when i run the jar file , getting following error :

2018-10-31 15:36:34.259 ERROR 8220 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[disp
atcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context
 with path [] threw exception [Request processing failed; nested exception is or
g.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringE
L expression: "greeting.id" (template: "result" - line 9, col 8)] with root caus
e

org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property o
r field 'id' cannot be found on null

I am not sure where the issue is, thymeleaf doc says that , model attributes can be accessed with ${attributeName} and i am also using is the same, but then why it says "id cannot be found"

please suggest

1
  • Are you sure greetingForm method gets called? Commented Oct 31, 2018 at 10:42

1 Answer 1

1

From Spring mvc doc, I quote-

What happens when a model attribute name is not explicitly specified? In such cases a default name is assigned to the model attribute based on its type. For example if the method returns an object of type Account, the default name used is "account". You can change that through the value of the @ModelAttribute annotation. If adding attributes directly to the Model, use the appropriate overloaded addAttribute(..) method - i.e., with or without an attribute name.

In your case,

@PostMapping("/greeting")
public String greetingSubmit(@ModelAttribute("greeting) RequestData greeting) {
..
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.