SSM框架实例(登陆功能)

阅读更多

SSM框架实例(用户登录功能)*需考虑tomcat与jdk版本兼容性问题

 

第一步:新建一个web工程 First,导入搭建SSM需要的jar包到WebContent\WEB-INF\lib。

 

第二步:创建需要的类包controller、mapper、pojo、service、serviceImpl;

 

1.controller包中放控制层java类:

 

Login.java

 

@Controller//注解

public class Login {

 

//@Autowired可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作

 

@Autowired//标注,注意这里的是接口,不是具体的实现类

public LoginService service;

 

@RequestMapping("/login")//映射地址可随意定义

private String login(String userName,String passWord){

User user=service.login(userName, passWord);

if(user!=null){

return "success";//返回jsp的文件名,不需要前缀和后缀,在springmvc.xml配置了前缀和后缀即可

}

return "fail";

}

}

 

 

2.mapper包中放dao层java接口和配置文件:

 

 

接口名称与相应的配置文件名一致,配置文件中的id对应接口中的方法名

配置文件的namespace就是对应接口的路径

 

LoginMapper.java

 

public interface LoginMapper {

 

//登陆

public User login(String userName,String passWord);

}

 

LoginMapper.xml

 

 

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  

"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 

 

 

 

 

3.pojo包中放置javabean:

 

 

User.java

 

public class User {

 

private String userName;

private String passWord;

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getPassWord() {

return passWord;

}

public void setPassWord(String passWord) {

this.passWord = passWord;

}

 

}

 

 

4.service包中放业务层接口,同mapper

 

 

LoginService.java

 

public interface LoginService {

 

//登陆

public User login(String userName,String passWord);

}

 

 

5.serviceImpl包中放业务层对应接口的实现类

 

 

LoginServiceImpl.java

 

public class LoginServiceImpl implements LoginService{

 

@Autowired

public LoginMapper mapper;

 

@Override

public User login(String userName, String passWord) {

User user=mapper.login(userName, passWord);

return user;

}

 

}

 

 

 

第三步:创建视图层,在WebContent\WEB-INF 创建文件夹view存放jsp页面(不能通过浏览器直接访问)

 

1.fail.jsp

2.success.jsp

 

在WebContent下创建login.jsp(可通过浏览器直接访问)

注意事项:form表单的action必须与控制层相应controller中相应的方法的@RequestMapping的值一致,

 控件的name与对应方法的形参一致。

 

<%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%>

login

Login


account
password

 

 

 

 

第四步:创建一个数据源文件夹,放置SSM相关的配置文件

 

 

1.sqlMapConfig.xml 配置文件

 

 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

 "http://mybatis.org/dtd/mybatis-3-config.dtd">

 

 

 

 

 

2.applicationContext-dao.xml 配置文件

 

 

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.0.xsd   

  http://www.springframework.org/schema/context   

  http://www.springframework.org/schema/context/spring-context-3.0.xsd  

  http://www.springframework.org/schema/mvc   

  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

  

 

 

 

 

 

 

 

 

 

3.applicationContext-service.xml 配置文件

 

 

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.0.xsd   

  http://www.springframework.org/schema/context   

  http://www.springframework.org/schema/context/spring-context-3.0.xsd  

  http://www.springframework.org/schema/mvc   

  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

 

 

 

 

 

4.applicationContext-transaction.xml 配置文件

 

 

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"  

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="   

  http://www.springframework.org/schema/beans   

  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   

  http://www.springframework.org/schema/context   

  http://www.springframework.org/schema/context/spring-context-3.0.xsd  

  http://www.springframework.org/schema/mvc   

  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

  http://www.springframework.org/schema/tx

  http://www.springframework.org/schema/tx/spring-tx.xsd  

  http://www.springframework.org/schema/aop

  http://www.springframework.org/schema/aop/spring-aop.xsd">

  

 

 

 

 

 

 

 

5.springmvc.xml 配置文件

 

 

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:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 

http://www.springframework.org/schema/mvc 

http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 

http://www.springframework.org/schema/context 

http://www.springframework.org/schema/context/spring-context-3.2.xsd 

http://www.springframework.org/schema/aop 

http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 

http://www.springframework.org/schema/tx 

http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

 

 

 

 

 

 

 

 

 

 

 

 

 

第五步:配置web.xml文件

 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 

contextConfigLocation

/WEB-INF/classes/spring/applicationContext-*.xml

org.springframework.web.context.ContextLoaderListener

 

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring/springmvc.xml

springmvc

/

 

 

 

 

第六步:将工程部署到tomcat,启动服务,通过http://localhost:8080/First/login.jsp链接到登陆页面。

 

 

 

 

 

你可能感兴趣的:(SSM框架实例(登陆功能))