利⽤SSM框架完成后台接⼝开发
@Controller
@RequestMapping("/User")
public class UserController {
@CrossOrigin(allowCredentials ="true")
@ResponseBody
@PostMapping("/Login")
public ResponseMessage login(String userId, String password, HttpServletRequest request){
...代码块...
}
}
当前端采⽤get⽅式时,⽤@GetMapping("/Login"),采⽤post⽅式时,⽤@PostMapping("/Login")(后⾯极有可能遇到session⽆法传递,这就是跨域问题,可以看本篇博⽂后⾯部份)
SSM框架集成了mybaties,Spring,Spring MVC,能实现接⼝开发
mybaties配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-////DTD Config 3.0//EN""/dtd/mybatis-3-config.dtd"> <configuration>
<!--别名配置把entity包中简单类起个别名(如User类的别名就是user)-->
<typeAliases>
<package name="entity"/>
</typeAliases>
</configuration>
Spring配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:p="/schema/p"
xmlns:context="/schema/context"
xmlns:mvc="/schema/mvc"
xmlns:jdbc="/schema/jdbc"
xmlns:jee="/schema/jee"
xmlns:aop="/schema/aop"
xmlns:tx="/schema/tx"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans.xsd
/schema/context
/schema/context/spring-context.xsd
/schema/mvc
/schema/mvc/spring-mvc.xsd
/schema/tx
/schema/tx/spring-tx.xsd
/schema/aop
/schema/aop/spring-aop.xsd">
<!--配置数据源(spring接管了mybatis的配置)-->
<!-- c3p0连接池-->
<bean id="dataSource"
class="hange.v2.c3p0.ComboPooledDataSource">
<!-- jdbc:mysql://localhost:3306/testdb?serverTimezone=UTC -->
<!--本地数据库可以省略localhost:3306-->
<property name="jdbcUrl"
value="jdbc:mysql://服务器ip/数据库名?serverTimezone=UTC"></property>
<property name="driverClass" value="sql.cj.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="*****数据库密码"></property>
<!--当连接池中的连接耗尽的时候c3p0⼀次同时获取的连接数,Default:3-->
<property name="acquireIncrement" value="3"></property>
<!--初始化时获取连接数,取值应在minPoolSize与maxPoolSize之间。Default:3-->
<property name="initialPoolSize" value="10"></property>
<property name="initialPoolSize" value="10"></property>
<property name="minPoolSize" value="2"></property>
<property name="maxPoolSize" value="10"></property>
</bean>
<!-- sqlSessionFactory配置(回忆⼀下mybatis编程,现在由spring注⼊)-->
<bean id="sqlSessionFactory"
class="batis.spring.SqlSessionFactoryBean">
<!--注⼊数据库连接池-->
<property name="dataSource" ref="dataSource"/>
<!--加载mybatis-config配置-->
<property name="configLocation"
value="classpath:l"/>
</bean>
<!--配置mybatis mapper批量扫描-->
<!--从basePackage指定的mapper包中扫描mapper类,⾃动⽣成bean -->
<bean class="batis.spring.mapper.MapperScannerConfigurer">
<!--注⼊sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName"
value="sqlSessionFactory"/>
<property name="basePackage" value="mapper"/>
</bean>
<!--事务配置(spring接管mybatis事务操作,如事务的提交、回滚、关闭等)-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--使⽤annotation注解⽅式配置事务-->
<tx:annotation-driven
transaction-manager="transactionManager"/>
<!--⾃动扫描配置-->
<!--在base-package包中扫描@Service、@Component注解的类,并把这些类⾃动注册为bean 备注:@Controller放到spring mvc扫描-->
<context:component-scan base-package="service"/>
<context:component-scan base-package="entity"/>
</beans>
Spring MVC配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:p="/schema/p"
xmlns:context="/schema/context"
xmlns:mvc="/schema/mvc"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans.xsd
/schema/context
/schema/context/spring-context.xsd
/schema/mvc
/schema/mvc/spring-mvc.xsd">
<!--启⽤注解映射+json转换器-->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.verter.StringHttpMessageConverter"/>
<bean class="org.verter.json.MappingJackson2HttpMessageConverter "/> </mvc:message-converters>
</mvc:annotation-driven>
<!--⾃动扫描-->
<!-- spring mvc⾃动扫描base-pack下或⼦包下的@Controller注解的类,⾃动注册为bean
注:@Service、@Component⼀般放到spring配置⽂件中去扫描-->
<context:component-scan base-package="controller"/>
<!--视图解析路径配置依赖jstl包-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="contentType" value="text/html"/>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--⽂件上传配置-->
<bean id="multipartResolver"
class="org.springframework.web.multipartmons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/><!--默认编码ISO-8859-1-->
<property name="maxInMemorySize" value="10240"/><!--最⼤内存10M -->
<property name="uploadTempDir" value="/Static-Resources/images/"/><!--上传的⽂件夹名字--> <property name="maxUploadSize" value="-1"/><!--最⼤⽂件,-1不限制-->
</bean>
<!--配置静态资源(JS、CSS、图⽚等)的访问路径-->
<!--对location⽂件夹下内容的访问将不再被DispatcherServlet拦截,在webapp下⾯兴建⽂件夹-->
<mvc:resources mapping="/images/**" location="/Static-Resources/images/"/>
<!--<mvc:resources mapping="/gif/**" location="/gif/"/>
<mvc:resources mapping="/jquery/**" location="/jquery/"/>
<mvc:resources mapping="/css/**" location="/Static-Resources/css/"/>
<mvc:resources mapping="/js/**" location="/Static-Resources/js/"/>-->
</beans>
然后下⾯介绍遇到的⼀系列问题
前端ajax请求始终error
跨域问题,需要在接⼝上⽅添加注解 @CrossOrigin(allowCredentials=“true”)
javax.servlet.http⽆法导⼊,导致session创建失败jquery框架使用
需要从Tomcat中
idea 打包war包程序包javax.servlet.http不存在
在l中添加依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
SSM静态⽂件存放位置
需要在Spring MVC配置中添加
<mvc:resources mapping="/images/**" location="/Static-Resources/images/"/>
⽂件上传时如果上传到Tomcat/weebapps/ROOT/upload/⽬录下
访问⽅式:服务器ip:8080/upload/3.jpg
跨域拦截配置
前后端分离时,会出现跨域问题
⼀⽅⾯会使后端数据不能送达前端
解决办法:在⽅法上添加注解
@CrossOrigin(allowCredentials="true")
另⼀⽅⾯,会出现sessionId不⼀致问题
解决办法:添加配置函数
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
// 设置跨域访问
@Override
public void addCorsMappings(CorsRegistry registry){
registry.addMapping("/**")
.
allowedOrigins("*")
.allowedMethods("GET","HEAD","POST","PUT","PATCH","DELETE","OPTIONS","TRACE") .allowCredentials(true);
}
}
前端ajax请求添加
xhrFields:{
withCredentials:true
}
希望有所帮助
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论