qq_charm 2018-05-10 06:28 采纳率: 33.3%
浏览 1757
已采纳

java SSM整合问题,无法进入Controller

图片说明
web.xml

 <context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>classpath:spring-mybatis.xml</param-value>  
  </context-param>
  <servlet>  
       <servlet-name>SpringMVC</servlet-name>  
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
       <init-param>  
           <param-name>contextConfigLocation</param-name>  
           <param-value>classpath:spring-mvc.xml</param-value>  
       </init-param>  
       <load-on-startup>1</load-on-startup>  
       <async-supported>true</async-supported>  
   </servlet>  
   <servlet-mapping>  
       <servlet-name>SpringMVC</servlet-name>  
       <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->  
       <url-pattern>/</url-pattern>  
   </servlet-mapping>

spring-mvc.xml:

 <beans xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
        xmlns:context="http://www.springframework.org/schema/context"  
        xmlns:mvc="http://www.springframework.org/schema/mvc"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans    
                            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
                            http://www.springframework.org/schema/context    
                            http://www.springframework.org/schema/context/spring-context-3.1.xsd    
                            http://www.springframework.org/schema/mvc    
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  
        <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
        <context:component-scan base-package="com.controller.*" /> 

        <!--避免IE执行AJAX时,返回JSON出现下载文件 -->  
        <bean id="mappingJacksonHttpMessageConverter"  
            class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  
            <property name="supportedMediaTypes">  
                <list>  
                    <value>text/html;charset=UTF-8</value>  
                </list>  
            </property>  
        </bean>  

Spring-mybatis.xml:

 <!-- 自动扫描 -->  
       <!-- <context:component-scan base-package="com.services.*" /> -->
         <!-- 引入配置文件 -->  
        <bean id="propertyConfigurer"  
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
            <property name="location" value="classpath:jdbc.properties" />  
        </bean>  

        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
            destroy-method="close">  
            <property name="driverClassName" value="${driver}" />  
            <property name="url" value="${url}" />  
            <property name="username" value="${username}" />  
            <property name="password" value="${password}" />  
            <!-- 初始化连接大小 -->  
            <property name="initialSize" value="${initialSize}"></property>  
            <!-- 连接池最大数量 -->  
            <property name="maxActive" value="${maxActive}"></property>  
            <!-- 连接池最大空闲 -->  
            <property name="maxIdle" value="${maxIdle}"></property>  
            <!-- 连接池最小空闲 -->  
            <property name="minIdle" value="${minIdle}"></property>  
            <!-- 获取连接最大等待时间 -->  
            <property name="maxWait" value="${maxWait}"></property>  
        </bean>  

        <!-- 第一种 spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->  
         <!-- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
            <property name="dataSource" ref="dataSource" />  
                自动扫描mapping.xml文件
            <property name="mapperLocations" value="classpath*:mapping/*.xml"></property>  
        </bean>  -->
        <!--第二种 mybatis-spring整合  手动配置mybatis配置文件-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
        </bean>
        <!-- DAO接口所在包名,Spring会自动查找其下的类 -->  
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
            <property name="basePackage" value="com.DAO" />  
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>  
        </bean> 

mybatis.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <!-- 创建实体类别名 -->
        <typeAliases>
            <!--type:对象类型  alias:对象别名 -->
            <typeAlias type="com.model.User" alias="User"/>
        </typeAliases>
        <!--配置mybatis映射文件  -->
        <mappers>
            <mapper resource="com/mapper/UserMapper.xml"/>
        </mappers>
    </configuration>

DAO:

 package com.DAO;

import com.model.User;

public interface UserDao {
    User findUserByName(String username,String pwd);
}

mapper:

 <mapper namespace="com.DAO.UserDao">  

      <!-- 新增 -->
      <insert id="saveUser" parameterType="com.cn.ssm.pojo.User"  >
         insert into t_user(user_name,user_age) values (#{username},#{age})
      </insert>

      <!-- 修改 -->
      <update id="updateUser" parameterType="com.cn.ssm.pojo.User" >
        update t_user set user_name=#{username},user_age=#{age} where user_id=#{id}
      </update>

      <!-- 删除 -->
      <delete id="deleteUser" parameterType="int">
          delete from t_user where user_id=#{id}    
      </delete>

      <!-- 根据id查找单个用户 -->
      <select id="findUserById" parameterType="java.lang.Integer" resultType="com.cn.ssm.pojo.User">
         select id,user_name,age from user_t where id=#{id}    
      </select>

      <!-- 查询所有 -->
       <select id="findAll" resultType="com.cn.ssm.pojo.User">    
         select user_id id,user_name userName,user_age age from t_user    
       </select>  

       <!-- 根据用户名和密码查询用户 -->
       <select id="findUserByName" parameterType="String" resultType="com.model.User">
            <!-- 只传一个参数到sql语句时,可以直接写参数名,当传多个参数时,需用0,1,2...,或者在dao层使用@Param注解-->
            select * from userinfo where UserName=#{0} AND PassWord=#{1}
       </select>

services:

 package com.services;

import com.model.User;

public interface IUserService {
    public User findUserByName(String username,String pwd);
}

services.implents:

 @Service
@Transactional
public class UserServiceImpl implements IUserService{
    @Autowired
    public UserDao UserMapper;
    public User findUserByName(String name,String password){
        return this.UserMapper.findUserByName(name, password);
    }
}

Controller:

 @Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @RequestMapping("longin")
    public void checkLogin(HttpServletRequest request,HttpServletResponse response){
        String username=request.getParameter("name");
        String password=request.getParameter("password");
        response.setCharacterEncoding("utf-8");
        System.out.println(username+","+password);
        User user=userService.findUserByName(username, password);
        HttpSession session=request.getSession();
        if(user!=null){
            session.setAttribute("user", user);
            try {
                //response.sendRedirect("../index2.jsp");
                response.getWriter().write("登录失败");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            try {
                response.getWriter().write("登录失败");
            } catch (IOException e) {
                // TODO Auto-generated catch block

jsp:

 <body>
    <form action="user/longin">
        <label>用户名:<input type="text" name="name"></label>
        <label>密码:<input type="password" name="password"></label>
        <input type="submit" value="登录...">
    </form>
    <button onclick="login_ip()">IP登录</button>
</body>
  • 写回答

13条回答 默认 最新

  • daojian_ 2018-05-10 09:44
    关注

    扫描路径后面不要写“.*”,还有视图解析器呢?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(12条)

报告相同问题?

悬赏问题

  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥20 和学习数据的传参方式,选择正确的传参方式有关
  • ¥15 这是网络安全里面的poem code
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错
  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥30 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路
  • ¥15 内网办公电脑进行向日葵