0

Below is some simple code.

@RequestMapping(value="/main")
public ModelAndView main(ModelAndView model) {
    
    model.addObject("project",mainService.selectProject());
    
    return null;
}

I thought until now that the role of parameter should be used only in the form of method name (x,y, etc..) elsewhere.

However, when used like the code above, it is not a role that receives a certain value, but rather a "one variable?" or "One constructor?"

It seems to be used like

It can be used as if using new, but I would like to know the principle.

Could you explain the principle or point me to a keyword that I can study?

===

public ModelAndView main(ModelAndView model) {

In the ModelAndView model is this part.

When this part calls the method (ex, method name (x, y))

I was only thinking of it for the purpose of receiving the value of something.

However, the ModelAndView model above can be used as a variable or constructor by itself.

From the line of knowledge I know, ModelAndView model = new ModelAndView(); should be However, this part was omitted and it can be used by declaring it in the parameter.

enter image description here

바보린
  • 159
  • 1
  • 13
  • Please clarify your question, I have no idea what you are asking. There is nothing constructor-like or new-like in the code you showed. If the annotation is not what you are asking about, remove it. – Mat Mar 01 '22 at 07:24
  • sorry. Edited the content. Also, my comment said that my question was considered a duplicate and replaced with an answer to another post instead. I don't know what this means, but I wrote it down because I thought I couldn't get a reply to my post. – 바보린 Mar 01 '22 at 07:35
  • I've reopened the question, but I have no idea what you're asking... Your method is declared to accept an input parameter of type `ModelAndView` and to return a result of type `ModelAndView` (or null, which it does in this case). – Robby Cornelissen Mar 01 '22 at 07:47
  • 1
    Sorry, this makes no sense at all – Software Engineer Mar 01 '22 at 07:49
  • Oh, sorry. Let me explain the question again. First of all, ModelAndView is a simple example, and return null is a temporary part for convenience. As you just commented, ModelAndView was used as a parameter. That's the part I'm curious about. – 바보린 Mar 01 '22 at 07:55
  • To the best of my knowledge, to use this "object variable" mian() { ModelAndView model = new ModelAndView(); model.addObject... } should be implemented like However, this long part is omitted. main(ModelAndView model) { model.addObject.. } has become I wonder how this omitted and the ModelAndView model written as a parameter can be used as an object variable. – 바보린 Mar 01 '22 at 07:55
  • I'm really sorry to bother you. I've attached a picture of the rewritten code for the exact question. – 바보린 Mar 01 '22 at 08:02
  • Looks like you're missing so Java basics, e.g. parameter passing (https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html) – Mat Mar 01 '22 at 08:59

0 Answers0