SSM整合中易出现的问题(xml文件无法自动加载,跨域,lib目录,乱码等)

记第一次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>
上一篇:Java实现CURL,与把字符串结果写到json文件


下一篇:HttpServletResponse对象