<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    沉睡森林@漂在北京

    本處文章除注明“轉(zhuǎn)載”外均為原創(chuàng),轉(zhuǎn)載請(qǐng)注明出處。

      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      152 隨筆 :: 4 文章 :: 114 評(píng)論 :: 0 Trackbacks

    Spring2.5MVC應(yīng)用實(shí)例

     

    首先在web.xml中配置spring相關(guān)servletlistener

           <!--  Spring 服務(wù)層的配置文件 -->

        <context-param>

            <param-name>contextConfigLocation</param-name>

            <param-value>classpath:applicationContext.xml,classpath:dataAccessContext-jdbc.xml</param-value>

        </context-param>

        

        <!--  Spring 容器啟動(dòng)監(jiān)聽(tīng)器 -->

        <listener>

            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

        </listener>

     

        <!--  Spring MVC Servlet,它將加載WEB-INF/annomvc-servlet.xml

               配置文件,以啟動(dòng)Spring MVC模塊-->

        <servlet>

            <servlet-name>greatwall</servlet-name>

            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

            <load-on-startup>0</load-on-startup>

        </servlet>

     

        <servlet-mapping>

            <servlet-name>greatwall</servlet-name>

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

    </servlet-mapping>

     

    配置里面主要涉及三個(gè)xml配置文件,分別對(duì)應(yīng)的代碼如下:

    applicationContext.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:context="http://www.springframework.org/schema/context"

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

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

           xsi:schemaLocation="http://www.springframework.org/schema/beans

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

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

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

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

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

          

           <context:annotation-config />

          

        <context:component-scan base-package="com.example" />

    </beans>

    dataAccessContext-jdbc.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:context="http://www.springframework.org/schema/context"

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

           xsi:schemaLocation="http://www.springframework.org/schema/beans

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

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

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

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

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

          

           <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

                  <property name="driverClassName" value="com.mysql.jdbc.Driver" />

                  <property name="url" value="jdbc:mysql://localhost/greatwall" />

                  <property name="username" value="root" />

                  <property name="password" value="sa" />

           </bean>

           <!-- ibatis sqlMapClient config -->

        <bean id="sqlMapClient"   class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">

            <property name="configLocation">

                <value>classpath:sql\sql-map-config.xml</value>

            </property>

            <property name="dataSource">

                <ref bean="dataSource"/>

            </property>   

        </bean>

    </beans>

    greatwall-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: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">

           <context:component-scan base-package="com.example" />

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

           <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/app/" p:suffix=".jsp" />

    </beans>

     

    其中,dataAccessContext-jdbc.xml這個(gè)文件是為了ibatis需要的,如果你使用的hibernate或者jdbc的話,可以先將里面的bean全部去掉,保留一個(gè)空的beans節(jié)點(diǎn)既可。

     

    實(shí)例的java類如下:

    package com.example.controller;

     

    import java.io.IOException;

    import java.io.PrintWriter;

     

    import javax.servlet.http.HttpServletResponse;

     

    import net.sf.json.JSONObject;

     

    import org.springframework.beans.factory.annotation.Autowired;

    import org.springframework.beans.factory.annotation.Qualifier;

    import org.springframework.stereotype.Controller;

    import org.springframework.web.bind.annotation.RequestMapping;

     

    import com.example.domain.Customer;

    import com.example.exception.CustomerException;

    import com.example.service.ICustomerService;

    import com.example.util.ListRange;

     

    @Controller

    public class CustomerController {

          

           @Autowired(required = false)

           @Qualifier("customerService")

           private ICustomerService customerService = null;

     

           @RequestMapping("/listCustomer.do")

           public void list(HttpServletResponse response,int start,int limit) {

                  try {

                         ListRange<Customer> listRange = new ListRange<Customer>();

                         listRange.setLimit(limit);

                         listRange.setStart(start);

                        

                         customerService.queryRecords(listRange);

                         response.setContentType("text/javascript;charset=UTF-8");

                         try {

                                PrintWriter out = response.getWriter();

                                out.write(JSONObject.fromObject(listRange).toString());

                         } catch (IOException e) {

                               

                         }

                        

                  } catch (CustomerException e) {

                         e.printStackTrace();

                  }

           }

     

           @RequestMapping("/deleteCustomer.do")

           public void delete(HttpServletResponse response,String ids) {

                  ListRange<Customer> result = new ListRange<Customer>();

                  result.setSuccess(true);

                  try {

                         customerService.delete(ids);

                  } catch (CustomerException e) {

                         e.printStackTrace();

                         result.setSuccess(false);

                  }

                  response.setContentType("text/javascript;charset=UTF-8");

                  try {

                         PrintWriter out = response.getWriter();

                         out.write(JSONObject.fromObject(result).toString());

                  } catch (IOException e) {

                        

                  }

           }

     

           @RequestMapping("/updateCustomer.do")

           public void update(HttpServletResponse response,Customer customer) {

                  System.out.println(customer);

                  ListRange<Customer> result = new ListRange<Customer>();

                  result.setSuccess(true);

                  try {

                         customerService.update(customer);

                  } catch (CustomerException e) {

                         e.printStackTrace();

                  }

                  response.setContentType("text/javascript;charset=UTF-8");

                  try {

                         PrintWriter out = response.getWriter();

                         out.write(JSONObject.fromObject(result).toString());

                  } catch (IOException e) {

                        

                  }

           }

     

           @RequestMapping("/selectCustomer.do")

           public void select() {

                 

           }

     

           @RequestMapping("/insertCustomer.do")

           public void insert(HttpServletResponse response,Customer customer) {

                  System.out.println(customer);

                  ListRange<Customer> result = new ListRange<Customer>();

                  result.setSuccess(true);

                  try {

                         customerService.insert(customer);

                  } catch (CustomerException e) {

                         e.printStackTrace();

                  }

                  response.setContentType("text/javascript;charset=UTF-8");

                  try {

                         PrintWriter out = response.getWriter();

                         out.write(JSONObject.fromObject(result).toString());

                  } catch (IOException e) {

                        

                  }

           }

     

    }

     

    Spring會(huì)自動(dòng)的將http提交的參數(shù)對(duì)應(yīng)到javabean上,這里使用了一個(gè)ICustomerService的注入操作,還有一些javabean,可以直接去掉,保留一個(gè)最簡(jiǎn)單的版本進(jìn)行測(cè)試。

     

    posted on 2008-12-22 13:40 王總兵 閱讀(2258) 評(píng)論(0)  編輯  收藏 所屬分類: Spring
    主站蜘蛛池模板: 永久免费毛片在线播放| 亚洲综合免费视频| 又黄又爽一线毛片免费观看 | 亚洲免费无码在线| 国产午夜鲁丝片AV无码免费| 亚洲精品无AMM毛片| 女人18特级一级毛片免费视频| 亚洲一区二区三区在线观看网站| 一个人免费高清在线观看| 亚洲久悠悠色悠在线播放| 四色在线精品免费观看| 朝桐光亚洲专区在线中文字幕| 国产免费人成视频在线观看| 日日狠狠久久偷偷色综合免费| 亚洲一级特黄无码片| 国产一级黄片儿免费看| 久久亚洲中文字幕精品有坂深雪 | 无码日韩精品一区二区免费| 亚洲av永久无码| 亚洲国产中文字幕在线观看 | 国产成人精品日本亚洲网址| 麻豆国产VA免费精品高清在线| 一级毛片免费观看不收费| 亚洲一区AV无码少妇电影☆| 男女作爱在线播放免费网站| 亚洲依依成人精品| 国产成人涩涩涩视频在线观看免费| 免费一级毛片在线播放视频免费观看永久 | 亚洲男人第一av网站| 国产精品视频永久免费播放| 无码一区二区三区亚洲人妻| 亚洲成色WWW久久网站| 青春禁区视频在线观看直播免费| 国产亚洲精彩视频| 亚洲成AV人片在线观看WWW| 中文字幕影片免费在线观看| 狠狠热精品免费观看| 久久久久亚洲av无码专区喷水 | 亚洲精品美女视频| 免费中文字幕在线观看| 久久精品电影免费动漫|