记第一次ssm整合出现的问题:
目录1.target目录的问题
如果出现找不到配置文件或者无法加载配置文件中的bean
,可以查看一下target
目录里是否有相关的xml
文件,如果无法自动生成,则在pom
文件里加上如下配置:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
2.web文件夹下的lib
目录问题:
如果在pom
文件里加入了新的依赖,要记得往lib
目录里手动添加新的jar包
,如果是新项目不能运行,也要检查一下WEB-INF
文件夹下是否有lib
目录,如果没有则手动添加。
3.关于某个bean找不到的问题:
- 检查引用的时候id是否写错,检查大小写。
- 引用
resources
文件下的配置文件时一定要加上classpath:
,否则会报错。
4.关于mybatis的mapper
文件:
-
resultType
要写全类名
,如果只想写类名应该在配置文件中加上以下信息:<typeAliases> <package name="com.xsyz.pojo"/> </typeAliases>
-
<mappers>
下的<mapper>
中的resource
里的路径要用/
分割,不是用.
分割。如下:<mappers> <mapper resource="com/xsyz/mapper/BookMapper.xml"/> </mappers>
5.乱码问题
在web.xml
中加入如下配置:
<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>
6.跨域请求问题:
1.controller
层加入此类
@Configuration
public class ProcessInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
httpServletResponse.setHeader("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
httpServletResponse.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
httpServletResponse.setHeader("X-Powered-By","Jetty");
String method= httpServletRequest.getMethod();
if (method.equals("OPTIONS")){
httpServletResponse.setStatus(200);
return false;
}
System.out.println(method);
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
}
2.xml
中加入以下配置:
<mvc:interceptors>
<bean class="controller.ProcessInterceptor"></bean>
</mvc:interceptors>