标准WEB项目配置

Maven项目可以通过配置依赖来加载Jar包,非Maven项目则需要直接将Jar包复制到/WEB-INF/lib目录下。据此,我们可以首先点击此处下载urule-console-pro模块与urule-core-pro模块所需要的第三方Jar包,将它们放到我们项目中的/WEB-INF/lib目录下,然后再到 https://search.maven.org/ 上查询最新的urule-core-pro与urule-console-pro版本,下载下来放到/WEB-INF/lib目录中即可。这样,一个传统的的Web项目中添加URule Pro相关Jar包的工作也就完成了。

如果是将URule Pro添加到一个已存在的标准Web项目,那么在添加第三方Jar包时要注意这些Jar包在当前项目中是否已存在,如果已经存在,这时通常的做法是保留高版本,切不可放多个版本不同的相同Jar包,否则运行可能会出现错误。

接下来我们开始进行web应用层面配置,对于Web层面的配置,无论是Maven项目还是标准项目都是一样的。

因为urule-console-pro模块架构在Spring之上的,所以需要加载urule-console-pro模块中提供的Spring配置文件,这个配置文件位于urule-console-pro对应的jar的classpath根下,名为urule-console-context.xml,所以如果我们的项目也是基于Spring的,那么可以打开一个项目中的Spring配置文件,在其中通过下面的代码导入urule-console-context.xml文件:

<import resource="classpath:urule-console-context.xml"/>

如果你的项目不是基于spring,那么就不能采用上面的import方式加载urule-console-pro中所需要的spring配置文件,这时我们需要打开web.xml,在其中添加下面的代码以加载urule-console-pro的spring配置文件:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:urule-console-context.xml</param-value>
</context-param>

前面说过,因为urule-console-pro模块依赖于urule-core-pro模块,所以也需要加载urule-core-pro模块中的spring配置文件,这个文件也位于urule-core-pro对应jar包的classpath根下,名为urule-core-context.xml,但我们这里在配置时却不需加载它,原因是这个名为urule-core-context.xml的spring配置文件在urule-console-context.xml中已经导入了,所以在有urule-console-pro的项目当中,就不需要再加载urule-core-context.xml,只需要加载urule-console-pro中的urule-console-context.xml文件即可。

通常情况下,我们建议在/WEB-INF目录下创建一个名为context.xml的标准的spring配置文件,在这个context.xml中导入urule-console-pro模块中的spring配置文件,这个context.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    ">
    <import resource="classpath:urule-console-context.xml"/>
</beans>

这样就需要将上面在web.xml中添加到listener做些修改,contextConfigLocation的值改成/WEB-INF/context.xml,如下面代码所示:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/context.xml</param-value>
</context-param>

最后我们还需要在项目的web.xml当中添加URule Pro中的一个Servlet,这个Servlet负责控制台中所有页面与服务端的交互,配置信息如下:

<servlet>
    <servlet-name>uruleServlet</servlet-name>
    <servlet-class>com.bstek.urule.console.URuleServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>uruleServlet</servlet-name>
    <url-pattern>/urule/*</url-pattern>
</servlet-mapping>

在上面的servlet配置当中,需要注意的是servlet-mapping中的url-pattern的值必须是/urule/*。 到这里,在项目中添加URule Pro的操作就完成了。

results matching ""

    No results matching ""