【Springmvc 2】----注解方式参数

来源:互联网 发布:clrc663 源码 编辑:程序博客网 时间:2024/05/17 23:45

 


Springmvc 之注解方式参数

 


 

      通过前一篇博文(【springmvc 1】----实践篇)我们了解到,支持springmvc注解开发方式有四种:@controller控制层,@service 业务层,@Repository持久层,@component组件; 其实我们常用的只是其中的两个: @Controller 和@RequestMapping。

 

    接受参数方面呢?HttpServletRequest 可以直接定义在参数的列表,可以使用;在参数列表上直接定义好接受的参数名称,只有参数名称能匹配的上就能接受所传过来的数据,可以自动转换成参数列表里的类型,注意的是值月类型之间是可以转换的。

               

代码时间篇:

            

 

web:

 

<?xml version="1.0" encoding="UTF-8"?>    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"        id="WebApp_ID" version="2.5">        <display-name>lsn_springmvcold414</display-name>                        <!-- 中央控制器,配制spring分发器servlet -->        <servlet>            <servlet-name>springmvc</servlet-name>            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>                        <init-param>                <param-name>contextConfigLocation</param-name>                <param-value>/WEB-INF/springmvc-Servlet.xml</param-value>            </init-param>                        </servlet>        <servlet-mapping>            <servlet-name>springmvc</servlet-name>            <url-pattern>*.do</url-pattern>        </servlet-mapping>            </web-app>  



springmvc-Servlet.xml: 

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd         http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-3.0.xsd         http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd         http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">                                                        <mvc:annotation-driven/>    <context:component-scan base-package="cn.itcast.springmvc"/>                                  <!--视图解释器,前缀+viewName+后缀:从webroot 到某一指定的文件夹 -->        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">           <!-- 视图 的前缀 -->           <property name="prefix" value="/WEB-INF/jsp/"></property>           <!--视图的后缀 -->           <property name="suffix" value=".jsp"></property>        </bean>        </beans> 


 

index.jsp:

 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body>    We see you springmvc with annotation this time!       </body></html>

 

 

TestController.java:

 

package cn.itcast.springmvc;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;//注解开发的一个例子@Controller//用来标注当前的springmvc控制层类/*@RequestMapping("/test")*/public class TestController {      @RequestMapping("/hello.do")    public String hello() {      System.out.println("see you springmvc with annotation!");     return "index";      }}


                  

 


页面显示结果如下:

         

                    

 

 

参数能自动转换:

    /**   * 在参数列表上要定义接受的参数名称,只要参数名称能匹配的上就能接受传过来的数。   * 可以自动转换参数列表里面的类型。注意的是,值与类型之间是可以转换的。   * @param name   * @return   */  @RequestMapping("/toPerson1.do")  public String toPerson1 (Integer name)  {            System.out.println(name);      return "index";  }  


 

 

其中的: String name 当参数是 Integer name时:name=45如下显示:


                         

  


                              

 

 

如果:在 Integer name情况下 将name=zhangsan就行不通了:


                 




添加信息:姓名、年龄、地址等等

      代码如下:

 @RequestMapping("/toPerson1.do")  public String toPerson1 (String name,Integer age,String address)  {            System.out.println(name+"" +age +"" +address+"");      return "index";  }}


 

访问:

            


 访问成功否?


                  

 

 

 

小结:

       显然访问是成功的,在参数列表上要定义接受的参数名称,只要参数名称能匹配的

上就能接受传过来的书,它们可以自动的转换参数列表里面的类型。值得注意的是:值

与类型之间是可以转换的。

 

 

 

 

 

 

 

 

0 0