Spring Web ,使用annotation和form标签库开发web应用(二)请求的映射

来源:互联网 发布:手机制作抽奖软件 编辑:程序博客网 时间:2024/05/20 01:37

摘要:讲述用户请求映射到controller不同方法的规则。在此之前,必须将web应用做好配置支持spring-web,并且支持annotations。

 

假设WEB-INF/web.xml中的spring servlet映射如下:

     <servlet-mapping>

         <servlet-name>icshtml</servlet-name>

         <url-pattern>/ics/*</url-pattern>

     </servlet-mapping>

关于基础配置请看前一篇。

 

1.     简单Get映射:

当浏览器输入http://somehost:8080/myapp/ics/login时,映射到如下方法:

     @RequestMapping(value = "/ login", method = RequestMethod.GET)

     public String  login() {

         //do something

         return "login";//返回的页面路径是在icshtml-servlet.xml中配置的

     }

 

2.     简单Post映射:

页面login.jsp中有如下:

<form:form modelAttribute="user" target="_self" >

<form:input path="username" />

<form:password path="password" size="20"/>

<input type="submit" value="登录" name="B1" >

</form:form>

用户点击按钮提交时,表单信息会以post方式填写到请求中,即使url一样映射也不会和get方式冲突

@RequestMapping(value = "/ login", method = RequestMethod.POST)

     public String  loginPost(@ModelAttribute("user") User myUser) {

         //do something

         //check myUser username password

         return "welcome";//返回的页面路径是在icshtml-servlet.xml中配置的

     }

 

 

 

3.     服务端为用户填充表单数据:

http://somehost:8080/myapp/ics/new_card 返回的页面要获得部分数据填充

     @RequestMapping(value = "/new_card", method = RequestMethod.GET)

     public String new_card(ModelMap model) {

//do something

CardForm myCardForm = new CardForm();

//填充myCardForm中的变量

//放到servletContext的sessionmap中,页面可以直接读取

         model.put("cardForm", myCardForm);

         return "new_card";

     }

在页面new_card.jsp中有如下:

其中card是cardForm的一个变量,用get方法获取validEndDate是card的一个变量;

Spring form标签可以直接读取cardForm中的变量,但是jstl的c:out不能,只能从model中读取

<form:form modelAttribute="cardForm" target="_self" >

<form:input path="card.validEndDate" size="37" />

<form:input path="card.memo3" size="37" />

<c:out value="${cardForm.state}" />

<input type="submit" value="提交" name="B1">

</form:form>

 

 

4.     带自定义参数的url:

Controller可以使用带参数@PathVariable("id")的方法,这样就可以通过url传递参数,如下为了获取card 信息进行编辑,通过给服务端传入id获取

http://somehost:8080/myapp/ics/edit_card/id/5 请求会映射到如下方法

     @RequestMapping(value = "/edit_card/id/{id}", method = RequestMethod.GET)

     public String edit_card(@PathVariable("id") String cardId, Model model) {

         //fetch card info by cardId, which mean “5”,and fill in myCardForm

         Model.put("cardForm", myCardForm);

         return "new_card";

     }

 

 

5.     带http参数的请求:

 

表单提交的时候不一定要用@ModelAttribute来封装,可以直接按http请求参数来映射,通过@RequestParam("iccardNumber")来指定http参数。

如页面表单如下:

<form:form method="POST" action="${pageContext.request.contextPath }/ics/card_num_new">

         <input type="text" name="iccardNumber" size="20" />

         <input type="submit" value="添加" name="newAdd" />

</form:form>

对应方法(iccardNumber对应是input 标签的name属性指定):

     @RequestMapping(value = "/card_num_new", method = RequestMethod.POST)

     public String card_num_new(@RequestParam("iccardNumber") String iccardNumber, ModelMap model) {

         System.out.println("cardNumberList()=" + cardNumberList.size());

         model.put("cardNumberList", cardNumberList);

         return "card_num_management";

     }

0 0
原创粉丝点击