`

Easy spring mvc

 
阅读更多

 

Easy spring mvc

         Spring 注解方式实现 MVC

1、  添加 spring 相应的 jar

2、  修改 web.xml

<? 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" >   

  < servlet >     

     < servlet-name > spring </ servlet-name >     

     < servlet-class >

org.springframework.web.servlet.DispatcherServlet

</ servlet-class >     

     < load-on-startup > 1 </ load-on-startup >     

  </ servlet >     

  < servlet-mapping >     

     < servlet-name > spring </ servlet-name >  

<!-- 这里在配成 spring,

下边也要写一个名为 spring-servlet.xml 的文件,

主要用来配置它的 controller -->   

     < url-pattern > *.do </ url-pattern >     

  </ servlet-mapping >      

</ web-app >  

3、  这里只是演示 srping MVC ,所以对 applicationContext.xml 未说明,如果要注入 dao,servic 、事务,还需要在 applicationContext.xml 进行添加

4、  创建 WEB-INF/spring-servlet.xml( 对应 web.xml servlet 的名字:创建 xxx-servlet.xml) ,这里使用注解的方式: spring-serlet.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:p = "http://www.springframework.org/schema/p"

    xmlns:context = "http://www.springframework.org/schema/context"

    xsi:schemaLocation = "

        http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context-2.5.xsd" >

    <!-- 这里要对应 controller(Action) 包名 -->

    < context:component-scan base-package = "org.springtest.mvc" />

   

</ beans >

 

5、  Controller 示例:

@Controller

public class UserController {

 

    /*****

      * 请求路径: login.do

      * @param uid   前台传递参数 ?uid="", 或表单,会添充至参数 uid

      * @param request

      * @param response

      * @throws IOException

      */

    @RequestMapping ( "/login.do" )

    public void login( @RequestParam ( "uid" )String uid,HttpServletRequest request,HttpServletResponse response) throws IOException {

       String u = uid;

       System. out .println(u);

      

       PrintWriter out = response.getWriter();

       out.println(u);

       out.flush();

       out.close();

      

    }

    /******

      * dispatcher user.jsp 页面

      * request 范围:保存了 "user"   对象,直接页面访问

      * @return

      */

    @RequestMapping ( "/getAll.do" )

    public ModelAndView getAll() {

       ModelAndView mv = new ModelAndView( "/user.jsp" );

       mv.addObject( "user" , "ddddddddd" );

       return mv;

    }

    /********

      * 使用 @ModelAttribute 可以直接添加 vo 自动填充 vo 参数中

      * 只能是基础类型或 string,

      * 如若是 data 类型需要 自定义数据绑定

      * @param pet

      * @param result

      * @param response

      */

    @RequestMapping ( "/user_save.do" )

    public void savePet( @ModelAttribute ( "pet" )Pet pet,BindingResult result ,HttpServletResponse response) {

        System. out .println(pet.getPid()+ ":" +pet.getPname()+ ":" +pet.getBirthday());

       System. out .println( "===================save a pet ===============================" );

    }

}

6、  VO è Pet.java 示例

7、  表单示例: à user.jsp

    < form action = "user_save.do" method = "post" >

    id: < input type = "text" name = "pid" >< br >

  <!-- 对应 pet pid 属性 以下同 -->

    pname: < input type = "text" name = "pname" >< br >

    pbirthday: < input type = "text" name = "birthday" >< br >

    isLive: < input type = "text" name = "isLive" >< br >

    < input type = "submit" >< input type = "reset" >

    </ form >

8、  解决上传表单有日期类型

1)   创建绑定类,实现接口 WebBindingInitializer 接口,实现其方法

    public void initBinder(WebDataBinder binder,

  WebRequest request) {

       // TODO Auto-generated method stub

       SimpleDateFormat sdf =

new SimpleDateFormat( "yyyy-MM-dd" );

       sdf.setLenient( false );

       binder.registerCustomEditor(Date. class ,

new CustomDateEditor(sdf, false ));

    }

2)         spring-servlet.xml 中进行配置

<!-- 通用 date 转换   -->

    < bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >

        < property name = "cacheSeconds" value = "0" />

        < property name = "webBindingInitializer" >

            < bean class = "org.springtest.mvc. 实现类名 " />

        </ property >    

    </ bean >

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics