开发者

Spring get value of dropbox, POST/GET?

this is my view for /weather:

JSP FILE

....
<form method="post" action="/spring/krams/show/city">
<select name="city">
<c:forEach items="${cities}" var="city">
    <option value="<c:out value="${city.id}" />"><c:out value="${city.city}" /></option>
</c:forEach>
</select>
<input type="submit" value="Test" name="submit" />
</form>
.....

PICTURE!!

Spring get value of dropbox, POST/GET?

this is my controller for the /weather:

    @RequestMapping(value = "/weather", method = RequestMethod.GET)
public String getCurrentWeather(Model model) {
    logger.debug("Received request to show cities page");

    // Attach list of subscriptions to the Model
    model.addAttribute("cities",  service.getAllCities());

    // This will resolve to /WEB-INF/jsp/subscribers.jsp
    return "weather";
}

this is my view for /city:

JSP FILE!

....
<h1>Cities</h1>
<c:out value="${city.city}" />
....

this is my controller for the /city:

    @RequestMapping(value = "/city", method = RequestMethod.GET)
public String getCurrentCity(Model model) {
    logger.debug("Received request to show cities page");


    model.addAttribute("city",  service.getCity(2));

    // This will resolve to /WEB-INF/jsp/citys.jsp
    return "city";
}

when i press the button, it should go to my /city page and show the city which i got from service.getCity(2) .

MY PROBLEM:

when i just go to the url /city it gets the second city from database..IT WORKS..getCity method works.开发者_如何学运维..but when i press the submit button, it does not work .. it gives me tons of error..but i think i'm just using the syntax wrong

MY QUESTION: basically i want it to pass the dropbox value to /city and in /city controller it should getCity(x) , at the moment i am using getCity(2) for testing . how can i do it?

ASK IF YOU HAVE QUESTIONS!!!


The method getCurrentCity is annotated with @RequestMapping with parameter method=RequestMethod.GET, change it to RequestMethod.POST

Also change your method signature to:

public String getCurrentCity(@RequestParam("city") int city_id, Model model)

and call your service's getCity method using city_id

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜