Home>

I want to enter the value entered in the pull-down when I return to the input screen with the back button (not the browser back) from the confirmation screen, but I can do it in the text box, but I can not pull it down. Where should I improve?

As a flow
After inputting on the input screen, submit to the confirmation screen. You can check the input contents there. At that time, when you press the back button to correct the input contents and return to the input screen, the entered contents are displayed in the text box, but the pull-down is "-----".
Example
Input screen Taro/Orange Next
On the confirmation screen, Taro/Orange is displayed and returns.
Input screen Taro/------ This is what it looks like.

The code is an excerpt of only the necessary parts. Complete.jsp is omitted.

Supplementary question
I would like to use ----- as the initial value, but that is

Is it okay?

<form: form modelAttribute = "sampleBean"
            action = "confirm.htm" method = "POST">
          <input type = "text" name = "hoge" value = "${sampleBean.hoge}">
          <select name = "fruits" value = ${sampleBean.fruits}>
              <option value = "">-----</option>
              <option value = "apple">apple</option>
              <option value = "orange">orange</option>
              <option value = "grape">grape</option>
          </select>
          <input type = "submit" name = "confirm" value = "Next">
      </form: form>
<form: form modelAttribute = "sampleBean"
          action = "complete.htm" method = "POST">
            ${sampleBean.hoge}
            ${sampleBean.fruits}
          <input type = "submit" name = "index" value = "back">
          <input type = "submit" name = "complete" value = "application">
      </form: form>
import lombok. *;
import org.springframework.stereotype. *;
@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SampleBean {
    protected String hoge;
    protected String fruits;
}
import org.springframework.web.servlet. *;
import org.springframework.web.bind.annotation. *;
import org.springframework.beans.factory.annotation. *;
import org.springframework.stereotype. *;
@Controller
@RequestMapping (value = "/")
public class SampleController {
    @Autowired
    @Qualifier (value = "sampleBean")
    protected SampleBean sampleBean;
    @RequestMapping (value = "index", method = RequestMethod.GET)
    public ModelAndView index ()
    {
        ModelAndView mv = new ModelAndView ("index");
        mv.addObject ("sampleBean", new SampleBean ());
        return mv;
    }
    // submit in index.jsp
    @RequestMapping (value = "confirm", method = RequestMethod.POST)
    public ModelAndView confirm
    (@ModelAttribute ("sampleBean") SampleBean
            sampleBean)
    {
        ModelAndView mv = new ModelAndView ("confirm");
        this.sampleBean.hoge = sampleBean.hoge;
        this.sampleBean.fruits = sampleBean.fruits;
        SampleBean sampleBean1 = this.sampleBean;
        mv.addObject ("sampleBean", sampleBean1);
        return mv;
    }
    @RequestMapping (value = "complete", params = "complete", method = RequestMethod.POST)
    public ModelAndView complete ()
    {
        ModelAndView mv = new ModelAndView ("complete");
        return mv;
    }
    @RequestMapping (value = "complete", params = "index", method = RequestMethod.POST)
    public ModelAndView completeindex ()
    {
        ModelAndView mv = new ModelAndView ("index");
        mv.addObject ("sampleBean", sampleBean);
        return mv;
    }
}
  • Answer # 1

    Supplementary question
    I would like to use ----- as the initial value, but that is

    Is it okay?

    I think that's fine.

    Similarly, if you want to display with "orange" selected, for example,

    is not it.

    In other words, if you want to be selectedselectedAdd attributes,
    If not, it will not be attached.

    thisifIt's okay to use a branch to do it, but
    With SpringMVC, I think it's possible to write in a simpler way.

    It's a little guesswork, butselectThe part of the element
    ↓ How about changing it to something like this?

    <form: select path = "fruits">
      <form: option value = "" label = "-----" />
      <form: option value = "apple" />
      <form: option value = "orange" />
      <form: option value = "grape" />
    </form: select>

    with this,valueThe attribute value issampleBean.fruitsIs equal tooptionAutomatically on the elementselectI wonder if it has attributes ... I wish I could get it ...