SSM框架集成-配置文件

一、SSM框架集成

  首先引入框架的对应核心库以及依

1.1创建jdbc.properties数据库资源文件

jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/ssm
jdbc.username = xxxx
jdbc.password = xxxx

1.2创建并配置Spring核心文件—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:mvc="http://www.springframework.org/schema/mvc"
	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.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc.xsd">

	<!-- 1.加载属性配置文件:如果属性文件中没有前缀,必须加上system-properties-mode="NEVER"  -->
	<context:property-placeholder location="classpath:jdbc.properties" />
	
	<!-- 2.将连接池对象交给Spring去管理 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}"></property>
		<property name="url" value="${jdbc.url}"></property>
		<property name="username" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
	
	<!-- 3.将SqlSessionFactory交给Spring去管理  -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="typeAliasesPackage" value="cn.itsource.domain,cn.itsource.query"></property>
		<property name="mapperLocations" value="classpath:cn/itsource/mapper/*Mapper.xml"></property>
	</bean>
	
	<!-- 4.将Mapper接口交给Spring去管理 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 指定mapper接口的包路径 -->
		<property name="basePackage" value="cn.itsource.mapper"></property>
	</bean>
	
	<!-- 5.扫描包路径 -->
	<context:component-scan base-package="cn.itsource.service"></context:component-scan>

	<!-- 测试Bean -->
	<bean id="now" class="java.util.Date"></bean>
	
</beans>

1.3配置SpringMVC核心文件—applicationContext-mvc.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:mvc="http://www.springframework.org/schema/mvc"
	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.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc.xsd">


	<!-- 1.静态资源放行 -->
	<mvc:default-servlet-handler />

	<!-- 2.扫描包路径:上下文组件扫描 ,Spring容器会去扫描cn.itsource这个包及其子包中所有的类, 如果发现类上有类似@Controller这样注解,就会创建该类的对象,并进行管理 -->
	<context:component-scan base-package="cn.itsource.controller"></context:component-scan>

	<!-- 3.开启Spring对Mvc的支持:能够使用@RequestMapping -->
	<mvc:annotation-driven></mvc:annotation-driven>

	<!-- 4.视图解析器:统一处理【webmvc】 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property><!-- 前缀 -->
		<property name="suffix" value=".jsp"></property><!-- 后缀 -->
	</bean>
	
</beans>

1.4配置web.xml

<?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">
  
  <welcome-file-list>
  	<welcome-file>/home/index.html</welcome-file>
  </welcome-file-list>
  
  <!--1.配置监听器:加载Spring的配置文件 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<!-- 2.配置前端控制器:加载SpringMvc的配置文件 -->
	<servlet>
		<servlet-name>DispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:applicationContext-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>DispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
	<!--3.配置过滤器:解决post请求的中文参数乱码问题-->
	<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>
	</filter>
	<filter-mapping>
		<filter-name>EncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
</web-app>
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要在项目中添加xxl-sso-client的依赖,可以通过Maven或者手动添加jar包的方式。然后,在Spring配置文件配置xxl-sso-client的相关信息,如下所示: ``` <!-- 配置xxl-sso-client --> <bean id="xxlSsoClient" class="com.xxl.sso.client.filter.XxlSsoClientFilter"> <property name="serverUrlPrefix" value="${sso.server.url.prefix}" /> <property name="clientId" value="${sso.client.id}" /> <property name="clientSecret" value="${sso.client.secret}" /> <property name="logoutPath" value="${sso.client.logout.path:/logout}" /> <property name="loginPath" value="${sso.client.login.path:/login}" /> <property name="excludes" value="${sso.client.excludes}" /> </bean> <!-- 配置拦截器 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**" /> <bean class="com.xxl.sso.client.filter.XxlSsoClientInterceptor"> <property name="xxlSsoClient" ref="xxlSsoClient" /> </bean> </mvc:interceptor> </mvc:interceptors> ``` 其中,`${sso.server.url.prefix}`为xxl-sso-server的地址前缀,`${sso.client.id}`和`${sso.client.secret}`为xxl-sso-client的身份识别信息,`${sso.client.logout.path}`和`${sso.client.login.path}`为退出登录和登录的路径,`${sso.client.excludes}`为不需要拦截的路径。 最后,在Controller中添加`@XxlSsoClient`注解,表示该接口需要进行身份认证。 ``` @Controller public class UserController { @XxlSsoClient @RequestMapping("/user/info") @ResponseBody public String userInfo(HttpServletRequest request) { // 获取用户信息 XxlSsoUser xxlUser = (XxlSsoUser) request.getAttribute(XxlSsoConstant.XXL_SSO_USER); return "user info: " + xxlUser.toString(); } } ``` 以上就是集成xxl-sso到SSM框架中的步骤。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值