博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ssm框架整合+Ajax异步验证
阅读量:5047 次
发布时间:2019-06-12

本文共 17977 字,大约阅读时间需要 59 分钟。

SSM框架是目前企业比较常用的框架之一,它的灵活性、安全性相对于SSH有一定的优势。说到这,谈谈SSM和SSH的不同点,这也是企业常考初级程序员的面试题之一。说到这两套框架的不同,主要是持久层框架Hibernate和MyBatis的不同和控制层框架SpringMVC和Struts2的不同。

 

Hibernate和MyBatis的不同主要体现这么几点:

1.自动化和半自动化:Hibernate的SQL语句自动生成不需要程序员编写,而MyBatis需要编写。

2.学习上:Hibernate入门比较难,而MyBatis入门非常容易。

3.可移植性:Hibernate可移植性好,对应不同的数据库通过改变方言可以直接用,而MyBatis可移植性差,对应不同的数据库需要书写不同的SQL语句

4.关系维护上:Hibernate映射关系复杂,而MyBatis相对简单。

5.缓存:Hibernate有更好的二级缓存,可以使用第三方缓存,而MyBatis本身缓存就不好。

 

 

SpringMVC和Struts2的不同点如下:

1.入口不同:SpringMVC的入口是Servlet,Struts的入口是Filter。

2.性能上:spring3 mvc是方法级别的拦截,拦截到方法后根据参数上的注解,把request数据注入进去,在spring3 mvc中,一个方法对应一个request上下文。而struts2框架是类级别的拦截,每次来了请求就创建一个Action,然后调用setter getter方法把request中的数据注入;struts2实际上是通 setter getter方法与request打交道的;struts2中,一个Action对象对应一个request上下文。  

3.拦截器实现机制上,Struts2有以自己的interceptor机制,SpringMVC用的是独立的AOP方式,这样导致Struts2的配置文件量还是比SpringMVC大。

4. 设计思想上,Struts2更加符合OOP的编程思想, SpringMVC就比较谨慎,在servlet上扩展。

5.SpringMVC集成了Ajax,使用非常方便,只需一个注解@ResponseBody就可以实现,然后直接返回响应文本即可,而Struts2拦截器集成了Ajax,在Action中处理时一般必须安装插件或者自己写代码集成进去,使用起来也相对不方便。

6.Spring MVC和Spring是无缝的。从这个项目的管理和安全上也比Struts2高(当然Struts2也可以通过不同的目录结构和相关配置做到SpringMVC一样的效果,但是需要xml配置的地方不少)。

第一步:导包(这一步只要成功,可以说成功了80%)

aopalliance.jaraspectjweaver-1.5.4.jarcommons-fileupload-1.3.1.jarcommons-io-2.4.jarcommons-logging-1.1.3.jarfastjson-1.2.13.jarjstl-1.2.jarmail.jarmybatis-3.3.1.jarmybatis-spring-1.2.4.jarmysql-connector-java-5.1.26-bin.jarspring-aop-4.2.3.RELEASE.jarspring-aspects-4.2.3.RELEASE.jarspring-beans-4.2.3.RELEASE.jarspring-context-4.2.3.RELEASE.jarspring-context-support-4.2.3.RELEASE.jarspring-core-4.2.3.RELEASE.jarspring-expression-4.2.3.RELEASE.jarspring-instrument-4.2.3.RELEASE.jarspring-instrument-tomcat-4.2.3.RELEASE.jarspring-jdbc-4.2.3.RELEASE.jarspring-jms-4.2.3.RELEASE.jarspring-messaging-4.2.3.RELEASE.jarspring-orm-4.2.3.RELEASE.jarspring-oxm-4.2.3.RELEASE.jarspring-test-4.2.3.RELEASE.jarspring-tx-4.2.3.RELEASE.jarspring-web-4.2.3.RELEASE.jarspring-webmvc-4.2.3.RELEASE.jarspring-webmvc-portlet-4.2.3.RELEASE.jarspring-websocket-4.2.3.RELEASE.jar

 

第二步:写Spring主配置文件以及MyBatis主配置文件

 

 

第三步:创建实体类

package com.blog.entity;public class User {		private Integer Id;	private String email;	private String userName;	private String password;	private Integer power;	public Integer getId() {		return Id;	}	public void setId(Integer id) {		Id = id;	}	public String getEmail() {		return email;	}	public void setEmail(String email) {		this.email = email;	}	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;	}	public Integer getPower() {		return power;	}	public void setPower(Integer power) {		this.power = power;	}		}

 第四步:创建接口以及SQL映射文件(通过动态代理的方式绑定)

package com.blog.mapper;import org.apache.ibatis.annotations.Param;import com.blog.entity.User;public interface UserMapper {		User Login(String email);		User Login2(@Param("email") String email,@Param("password") String password);}

 

第五步:创建Service接口以及实现类

package com.blog.service;import com.blog.entity.User;public interface UserService {		User Login(String email);	User Login2(String email,String password);		}

 

package com.blog.service.impl;import javax.annotation.Resource;import org.springframework.stereotype.Service;import com.blog.entity.User;import com.blog.mapper.UserMapper;import com.blog.service.UserService;@Servicepublic class UserServiceImpl implements UserService{    @Resource    private UserMapper userMapper;            @Override    public User Login(String email) {        // TODO Auto-generated method stub        return userMapper.Login(email);    }    @Override    public User Login2(String email, String password) {        // TODO Auto-generated method stub        return userMapper.Login2(email, password);    }        }

第六步:写测试类测试上述方法

package com.blog.test;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.blog.entity.User;import com.blog.mapper.UserMapper;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class testMapper {    @Resource    private UserMapper userMapper;                @Test    public void testName() throws Exception {                User user=userMapper.Login("1933108196@qq.com");        System.out.println(user);                                     }        @Test    public void testName2() throws Exception {                User user=userMapper.Login2("1933108196@qq.com", "kangri123");        System.out.println(user.getUserName());                                    }    }

第七步:测试成功后,开始设置web.xml

Demo_Model
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
CharacterEncoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
CharacterEncoding
/*
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
springmvc
*.do
Login.jsp

第八步:配置springmvc.xml(与Spring框架无缝整合)

配置完毕后记得添加Controller 相关的类

package com.blog.controller;import java.io.IOException;import java.io.PrintWriter;import java.util.HashMap;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import com.alibaba.fastjson.JSON;import com.blog.entity.User;import com.blog.service.UserService;@Controllerpublic class UserController {	@Resource	private UserService userService;		@RequestMapping(value="emailCheck2.do",method=RequestMethod.POST)	public void emailCheck2(String email,HttpServletResponse response,HttpServletRequest request,HttpSession session,Model model) throws IOException{		response.setContentType("text/html;charset=utf-8");		PrintWriter out=response.getWriter();		User user=userService.Login(email);		if(user!=null){			out.println("邮箱已经存在");		}else{			out.println("邮箱可以使用");		}				out.flush();		out.close();			}			@RequestMapping(value="emailCheck.do",method=RequestMethod.POST)	public void emailCheck(String email,HttpServletResponse response,HttpServletRequest request,HttpSession session,Model model) throws IOException{		response.setContentType("text/html;charset=utf-8");		PrintWriter out=response.getWriter();		User user=userService.Login(email);		if(user!=null){			out.println("邮箱正确");		}else{			out.println("邮箱错误");		}				out.flush();		out.close();			}		@RequestMapping(value="passCheck.do",method=RequestMethod.POST)	public void emailCheck(String email,String password,HttpServletResponse response,HttpServletRequest request,HttpSession session,Model model) throws IOException{		response.setContentType("text/html;charset=utf-8");		PrintWriter out=response.getWriter();		User user=userService.Login2(email,password);		if(user!=null){			out.println("密码正确");	        		}else{			out.println("密码错误");		}				out.flush();		out.close();			}	     @RequestMapping(value="LoginCheck.do",method=RequestMethod.POST)	 public String LoginCheck(String email,String password,HttpServletResponse response,HttpServletRequest request,HttpSession session,Model model){    	 User user=userService.Login2(email, password);    	 if(user==null){    		 request.setAttribute("error", "用户名或密码不能为空");    		 return "Login";    	 }else{    		     		 model.addAttribute("user", user);    		 return "index";    	 }    	      }		}

 

 

第十步:开始启动tomcat,如果控制台无报错信息,说明配置整合成功,反之失败,所以整合过程中一定要仔细

十月 28, 2017 12:06:40 上午 org.apache.tomcat.util.digester.SetPropertiesRule begin警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Demo_Model' did not find a matching property.十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log信息: Server version:        Apache Tomcat/7.0.82十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log信息: Server built:          Sep 29 2017 12:23:15 UTC十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log信息: Server number:         7.0.82.0十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log信息: OS Name:               Windows 8十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log信息: OS Version:            6.2十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log信息: Architecture:          amd64十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log信息: Java Home:             D:\jdk1.7.0_51\jre十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log信息: JVM Version:           1.7.0_51-b13十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log信息: JVM Vendor:            Oracle Corporation十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log信息: CATALINA_BASE:         D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log信息: CATALINA_HOME:         D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log信息: Command line argument: -Dcatalina.base=D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log信息: Command line argument: -Dcatalina.home=D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log信息: Command line argument: -Dwtp.deploy=D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82\wtpwebapps十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log信息: Command line argument: -Djava.endorsed.dirs=D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82\endorsed十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log信息: Command line argument: -Dfile.encoding=GBK十月 28, 2017 12:06:40 上午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: D:\jdk1.7.0_51\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;D:/jdk1.7.0_51/bin/../jre/bin/server;D:/jdk1.7.0_51/bin/../jre/bin;D:/jdk1.7.0_51/bin/../jre/lib/amd64;D:\jdk1.7.0_51\bin;D:\Maven\apache-maven-3.3.9\bin;C:\Program Files\Git\cmd;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;D:\Program Files\MySQL\MySQL Server 5.7\bin;C:\Program Files\VisualSVN Server\bin;C:\Program Files\TortoiseSVN\bin;D:\Program Files\mongodb-win32-x86_64-2.4.3\bin;C:\Users\tracholar\AppData\Roaming\cabal\bin;C:\Users\tracholar\AppData\Roaming\npm;D:\Program Files\userbin;D:\wamp64\bin\php\php5.6.16;D:\texlive\2015\bin\win32;D:\Program Files\eclipse;;.十月 28, 2017 12:06:40 上午 org.apache.coyote.AbstractProtocol init信息: Initializing ProtocolHandler ["http-bio-8080"]十月 28, 2017 12:06:40 上午 org.apache.coyote.AbstractProtocol init信息: Initializing ProtocolHandler ["ajp-bio-8009"]十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.Catalina load信息: Initialization processed in 1592 ms十月 28, 2017 12:06:40 上午 org.apache.catalina.core.StandardService startInternal信息: Starting service Catalina十月 28, 2017 12:06:41 上午 org.apache.catalina.core.StandardEngine startInternal信息: Starting Servlet Engine: Apache Tomcat/7.0.82十月 28, 2017 12:06:46 上午 org.apache.catalina.startup.TldConfig execute信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.十月 28, 2017 12:06:46 上午 org.apache.catalina.core.ApplicationContext log信息: No Spring WebApplicationInitializer types detected on classpath十月 28, 2017 12:06:46 上午 org.apache.catalina.core.ApplicationContext log信息: Initializing Spring root WebApplicationContext十月 28, 2017 12:06:46 上午 org.springframework.web.context.ContextLoader initWebApplicationContext信息: Root WebApplicationContext: initialization started十月 28, 2017 12:06:46 上午 org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh信息: Refreshing Root WebApplicationContext: startup date [Sat Oct 28 00:06:46 CST 2017]; root of context hierarchy十月 28, 2017 12:06:46 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [applicationContext.xml]十月 28, 2017 12:06:49 上午 org.springframework.web.context.ContextLoader initWebApplicationContext信息: Root WebApplicationContext: initialization completed in 2875 ms十月 28, 2017 12:06:49 上午 org.apache.catalina.startup.HostConfig deployDirectory信息: Deploying web application directory D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82\webapps\docs十月 28, 2017 12:06:49 上午 org.apache.catalina.startup.HostConfig deployDirectory信息: Deployment of web application directory D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82\webapps\docs has finished in 234 ms十月 28, 2017 12:06:49 上午 org.apache.catalina.startup.HostConfig deployDirectory信息: Deploying web application directory D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82\webapps\examples十月 28, 2017 12:06:51 上午 org.apache.catalina.core.ApplicationContext log信息: ContextListener: contextInitialized()十月 28, 2017 12:06:51 上午 org.apache.catalina.core.ApplicationContext log信息: SessionListener: contextInitialized()十月 28, 2017 12:06:51 上午 org.apache.catalina.core.ApplicationContext log信息: ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache@a8e6df5')十月 28, 2017 12:06:51 上午 org.apache.catalina.startup.HostConfig deployDirectory信息: Deployment of web application directory D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82\webapps\examples has finished in 1,568 ms十月 28, 2017 12:06:51 上午 org.apache.catalina.startup.HostConfig deployDirectory信息: Deploying web application directory D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82\webapps\host-manager十月 28, 2017 12:06:51 上午 org.apache.catalina.startup.HostConfig deployDirectory信息: Deployment of web application directory D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82\webapps\host-manager has finished in 421 ms十月 28, 2017 12:06:51 上午 org.apache.catalina.startup.HostConfig deployDirectory信息: Deploying web application directory D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82\webapps\manager十月 28, 2017 12:06:51 上午 org.apache.catalina.startup.HostConfig deployDirectory信息: Deployment of web application directory D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82\webapps\manager has finished in 141 ms十月 28, 2017 12:06:51 上午 org.apache.catalina.startup.HostConfig deployDirectory信息: Deploying web application directory D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82\webapps\ROOT十月 28, 2017 12:06:51 上午 org.apache.catalina.startup.HostConfig deployDirectory信息: Deployment of web application directory D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82\webapps\ROOT has finished in 94 ms十月 28, 2017 12:06:51 上午 org.apache.coyote.AbstractProtocol start信息: Starting ProtocolHandler ["http-bio-8080"]十月 28, 2017 12:06:51 上午 org.apache.coyote.AbstractProtocol start信息: Starting ProtocolHandler ["ajp-bio-8009"]十月 28, 2017 12:06:51 上午 org.apache.catalina.startup.Catalina start信息: Server startup in 11060 ms

 

 接下来开始写登录页面做异步验证,在此之前AJax所需的jQuery插件一定要记得导,导入后,写个alert弹框测试一下,以保证在Ajax和JQuery交互的过程中不会因为插件的问题而报错

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
登录

${error}

Email:
Password:
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
Insert title here${user.userName}

转载于:https://www.cnblogs.com/youcong/p/7745731.html

你可能感兴趣的文章
TensorFlow2.0矩阵与向量的加减乘
查看>>
NOIP 2010题解
查看>>
javascript中的each遍历
查看>>
String中各方法多数情况下返回新的String对象
查看>>
浅谈tcp粘包问题
查看>>
UVA11524构造系数数组+高斯消元解异或方程组
查看>>
排序系列之——冒泡排序、插入排序、选择排序
查看>>
爬虫基础
查看>>
jquery.lazyload延迟加载图片第一屏问题
查看>>
HDU 1011 Starship Troopers (树形DP)
查看>>
手把手教你写DI_1_DI框架有什么?
查看>>
.net常见的一些面试题
查看>>
OGRE 源码编译方法
查看>>
上周热点回顾(10.20-10.26)
查看>>
C#正则表达式引发的CPU跑高问题以及解决方法
查看>>
云计算之路-阿里云上:“黑色30秒”走了,“黑色1秒”来了,真相也许大白了...
查看>>
APScheduler调度器
查看>>
设计模式——原型模式
查看>>
如何一个pdf文件拆分为若干个pdf文件
查看>>
web.xml中listener、 filter、servlet 加载顺序及其详解
查看>>