Spring2.5MVC应用实例

Spring2.5MVC应用实例

Spring2.5MVC应用实例

 

首先在web.xml中配置spring相关servletlistener

       <!--  Spring 服务层的配置文件 -->

    <context-param>

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

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

    </context-param>

    

    <!--  Spring 容器启动监听器 -->

    <listener>

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

    </listener>

 

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

           配置文件,以启动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>

 

配置里面主要涉及三个xml配置文件,分别对应的代码如下:

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这个文件是为了ibatis需要的,如果你使用的hibernate或者jdbc的话,可以先将里面的bean全部去掉,保留一个空的beans节点既可。

 

实例的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会自动的将http提交的参数对应到javabean上,这里使用了一个ICustomerService的注入操作,还有一些javabean,可以直接去掉,保留一个最简单的版本进行测试。

 

你可能感兴趣的:(Spring2.5MVC应用实例)