SSM框架常用配置

配置web.xml

位于\WebRoot\WEB-INF\web.xml

通常需要改动的地方:

  • <display-name>
  • <welcome-file-list>下的<welcome-file>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <!-- 名称 -->
    <display-name>lnuAss</display-name>
    <!-- 配置默认页面 -->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

     <!-- 加载spring配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
    </context-param>
    <!-- 整合spring -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 配置struts核心拦截器 -->
    <filter>
        <!-- Filter的名字 -->
        <filter-name>struts2</filter-name>
        <!-- Filter的实现类 struts2.5以前可能有所不同 -->
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <!-- 拦截所有的url -->
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 字符编码过滤器 -->  
    <filter>  
        <filter-name>encodingFilter</filter-name>  
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
                <init-param>  
                    <param-name>encoding</param-name>  
                    <param-value>utf-8</param-value>  
                </init-param>  
                <init-param>  
                    <param-name>forceEncoding</param-name>  
                    <param-value>true</param-value>  
            </init-param>  
        </filter>  
        <filter-mapping>  
           <filter-name>encodingFilter</filter-name>  
           <url-pattern>/*</url-pattern>  
        </filter-mapping>  
</web-app>

配置struts.xml

位于\src\struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <!--设置拦截器与公共返回界面-->
    <package name=<!--自己定义的包名--> extends="struts-default">
        <interceptors>
            <interceptor name=<!--自己定义的拦截器名-->  class=<!--拦截器功能类--> >
                <param name="excludeMethods"><!--这里填写的函数不拦截--></param>
                <param name="includeMethods"><!--这里填写的函数必拦截--></param>
            </interceptor>
            <!--定义拦截器栈-->
            <interceptor-stack name="myinterceptor">
                <interceptor-ref name="defaultStack" />
                <interceptor-ref name=<!--自己定义的拦截器名--> />
            </interceptor-stack>

        </interceptors>
        <!--定义默认拦截器-->
        <default-interceptor-ref name="myintercepto"></default-interceptor-ref>
        <global-results>
            <!--设置登陆鉴权未通过的返回页面-->
            <result name="login">login.jsp</result>  
        </global-results>
    </package>
    <!--从外部引用具体的action配置-->
    <include file="config/struts-user.xml"></include>
</struts>

拦截器类

/*package与import省略*/
//继承于MethodFilterInterceptor,针对方法进行拦截
public class AuthorityInterceptor extends MethodFilterInterceptor {

    private static final long serialVersionUID = 1L;

    //判断请求是否是ajax请求
    private boolean isAjaxRequest(HttpServletRequest request) {  
        String header = request.getHeader("X-Requested-With");  
        if (header != null && "XMLHttpRequest".equals(header))  
            return true;  
        else 
            return false;  
    }

    //拦截处理
    @Override
    protected String doIntercept(ActionInvocation invocation){
        try{
            //System.out.println("拦截器启动");
            ActionContext ctx=invocation.getInvocationContext();
            Map session=ctx.getSession();
            HttpServletRequest rq = ServletActionContext.getRequest();
            String uri = rq.getRequestURI(); 
            /*
                这里执行某些功能
            */
            //如果是ajax请求,则设置编码返回信息,如果不是ajax请求,则将回调地址写入session
            if(isAjaxRequest(rq))
            {
                HttpServletResponse rs=ServletActionContext.getResponse();
                rs.setCharacterEncoding("utf-8");
                /*
                    这里执行某些功能
                    比如
                    rs.getWriter().write("false");
                    return null;
                */

            }
            else
            {
                //设置登录后回调地址
                String url = rq.getScheme()+"://"+rq.getServerName()+":"+rq.getServerPort()+ rq.getRequestURI();
                rq.getSession().setAttribute("redirect-url", url);//session中回调url的key定为redirect-url
                return Action.LOGIN;
            }
        }catch(Exception e){
            e.printStackTrace();
            //拦截系统异常,统一抛到错误页
        }
        return null;
    } 

}

配置struts.xml中include的xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name=<!--包名--> namespace=<!--定义该包的命名空间--> extends=<!--继承前面定义了拦截器的包-->>
        <action name="" class="" method=">
            <result name=""><!--返回页面--></result>
        </action>
    </package>    
</struts>

struts2框架下的jsp文件

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="zh-cn">
    ……
</html>

配置applicationContext.xml

  • 位于\src\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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--注解自动注入-->
    <!-- 配置需要交给spring扫描管理的包,一般是包括整个项目的java文件的父包(由context提供) -->
    <!-- 在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的Java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean-->
    <!--这些bean会自动注入到-->
    <!--@Autowired-->
    <!--@Qualifier-->
    <!--@Resource等标签-->
    <context:component-scan base-package="lnu.lzs.serviceImpl"></context:component-scan>




    <!-- 配置需要交给spring扫描管理的文件,一般是项目的配置文件(由context提供) -->
    <context:property-placeholder location="classpath:jdbc.properties" />

    <!-- 配置数据源 -->
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${url}" />
        <property name="user" value="${user}" />
        <property name="driverClass" value="${driver}" />
        <property name="password" value="${password}" />

        <property name="minPoolSize" value="5" />
        <property name="maxPoolSize" value="100" />
        <property name="initialPoolSize" value="5" />
        <property name="maxIdleTime" value="60" />
    </bean>

    <!-- 事务管理 -->
    <bean name="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 配置sqlSessionFactory(由mybatis-spring.jar提供支持) -->
    <bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 配置数据源 -->
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath:lnu/lzs/dao/*.xml"></property>    
    </bean>


    <!-- Mapper接口所在包名(lnu.lzs.dao),Spring会自动查找其下(lnu.lzs.dao)的Mapper -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="lnu.lzs.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>

    <!-- 手动将Mapper接口注册为bean -->
<!--    <bean id="daoInterface" class="org.mybatis.spring.mapper.MapperFactoryBean"> -->
<!--        <property name="mapperInterface" value="lnu.lzs.dao.DaoInterface" /> -->
<!--        <property name="sqlSessionFactory" ref="sqlSessionFactory" /> -->
<!--    </bean> -->



</beans>  

jdbc.properties

driver=com.mysql.jdbc.Driver
url=
user=
password=

*Mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mapper namespace=<!-- 对应的接口类-->>
     <!-- 查询:id对应接口的函数名,resultType表示返回的JavaBean -->
    <select id="" parameterType="map" resultType="">
    </select>

    <!-- 增添:id对应接口的函数名-->
    <insert id="" parameterType="map" >
    </insert>

    <!-- 更新:id对应接口的函数名-->
    <update id="" parameterType="map">
    </update>

    <!-- 更新:id对应接口的函数名-->
    <delete id="deleteOrder" parameterType="map">
    </delete>


</mapper>

对应接口类

例如:

package lnu.lzs.dao;
public interface DaoInterface {
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值