Dorado 7 : 24.3.2 准备后台服务

Dorado7的Web服务是依托Spring MVC构建起来的。在web.xml的配置中我们可以找一个com.bstek.dorado.web.servlet.DoradoServlet的配 置,com.bstek.dorado.web.servlet.DoradoServlet即是继承自 org.springframework.web.servlet.DispatcherServlet的,因此我们可以直接借用因此而引入的 SpringMVC特性。当然,由于被Dorado7作了简单的包装,使用方法与标准的SpringMVC会略有区别。

步骤1:配置url-mapping

在web.xml中可以看到,默认的配置方式里doradoServlet只拦截了*.d、*,dpkg、/dorado/*三种URL,这三种都是 Dorado7系统需要使用的。为了在其中加入我们自己的Web服务,最好再增加一个自己的url-mapping。例如我们在其中追加一个*.do,完整代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <listener>
            <listener-class>com.bstek.dorado.web.servlet.SpringContextLoaderListener</listener-class>
    </listener>
    <filter>
        <filter-name>OpenSessionInViewFilter</filter-name>  
                <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>excludeSuffixs</param-name>
            <param-value>js,css,jpg,gif,png</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <url-pattern>*.d</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <url-pattern>/dorado/*</url-pattern>
    </filter-mapping>
        <filter-mapping>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <url-pattern>*.do</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>doradoServlet</servlet-name>
        <servlet-class>com.bstek.dorado.web.servlet.DoradoServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>doradoServlet</servlet-name>
        <url-pattern>*.d</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>doradoServlet</servlet-name>
        <url-pattern>*.dpkg</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>doradoServlet</servlet-name>
        <url-pattern>/dorado/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
    <servlet-name>doradoServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

 

步骤2:配置servlet-context.xml

WEB-INF/dorado-home/servlet-context.xml是Dorado7提供的一种Spring配置文件,专门用于配置与 DoradoServlet相关的内容。这里我们配置servlet-context.xml的目的是为了给*.do的请求指派一个处理器。这里说的处 理器通常应该是一个org.springframework.web.servlet.mvc.AbstractController的实现类。在这里我们配置登录实现类,最终代码如下:

<?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:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <bean class="com.bstek.dorado.uploader.DefaultUploadProcessor">
        <property name="uploadDir" value="{webapp.home}/images/photo"></property>
    </bean>
    
    <bean parent="dorado.resolverRegister"> 
        <property name="url" value="/login.do" /> 
        <property name="resolver"> 
            <bean class="com.bstek.dorado.sample.standardlesson.middle.LoginActionResolver"/> 
        </property> 
    </bean> 
</beans>

 

步骤3:完成登录实现类

在com.bstek.dorado.sample.standardlesson.middle包下新建LoginActionResolver,最终代码如下:

package com.bstek.dorado.sample.standardlesson.middle;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import com.bstek.dorado.sample.standardlesson.dao.SlEmployeeDao;
import com.bstek.dorado.sample.standardlesson.entity.SlEmployee;
import com.bstek.dorado.web.DoradoContext;
public class LoginActionResolver extends AbstractController {
    @Resource
    private SlEmployeeDao slEmployeeDao;
    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        SlEmployee employee = isValid(username, password);
        if (employee!=null) {
            request.getSession().setAttribute("user", employee);
            return new ModelAndView(
                    "com.bstek.dorado.sample.standardlesson.middle.main.Main.d");
        } else {
            String errormsg = "用户名或者密码不正确";
            ModelAndView mav = new ModelAndView(
                    "com.bstek.dorado.sample.standardlesson.middle.Login.d");
            mav.addObject("errormsg", errormsg);
            return mav;
        }
    }
    // 以下方法需要替换为自己的验证用户名密码的业务逻辑
    public SlEmployee isValid(String username, String password) {
        DetachedCriteria dc = DetachedCriteria.forClass(SlEmployee.class);
        if (username != null && !"".equals(username)) {
            dc.add(Restrictions.eq("userName", username.toUpperCase()));
        }
        List<SlEmployee> employees = slEmployeeDao.find(dc);
        for(SlEmployee employee:employees){
            if(password.equals(employee.getPassword())){
                return employee;
            }else
                return null;
        }
        return null;
    }
}

在本节中,没有对SpringMVC的具体实现方式做详细讲解,读者有兴趣或者有不清楚之处请自行google关键字进行知识储备。

note

由于项目配置情况的不同,在类中SlEmployeeDao有可能无法通过@Resource注解进行注入,在此时我们可以改用在项目servlet-context.xml中进行配置,将SlEmployeeDao注入到LoginActionResolver中。

 

到目前为止后台服务配置完毕,接下来我们查看运行效果。