1、创建工程:
2、创建数据库:(这里使用Mysql)
3、创建表及插入数据:
4、在工程中导入struts2和spring的相关包:
5、导入Jquery的包:
6、编写相应的Java文件:
BaseAction:
-
-
-
- package com.gzedu.base;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.apache.struts2.ServletActionContext;
-
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
-
-
-
-
-
- public class BaseAction extends ActionSupport {
- protected ActionContext ctx = ActionContext.getContext();
-
-
-
- private static final long serialVersionUID = 1L;
-
-
-
-
- protected HttpServletResponse getResponse(){
- HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);
- return response;
- }
-
-
-
-
- protected HttpServletRequest getRequest(){
- HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
- return request;
- }
- }
LoginAction类:
-
-
-
- package com.gzedu.login.action;
-
- import java.util.Map;
-
- import org.apache.struts2.json.JSONUtil;
-
- import com.gzedu.base.BaseAction;
- import com.gzedu.login.entity.Login;
- import com.gzedu.login.service.ILoginService;
-
-
-
-
-
- public class LoginAction extends BaseAction {
- private ILoginService loginService;
- private Login login;
-
-
-
- private static final long serialVersionUID = 1L;
-
-
-
-
- public String login() throws Exception{
- return SUCCESS;
- }
-
-
-
-
- public String checkLogin()throws Exception{
- Map<String,String> msgMap = loginService.login(login);
- boolean isLogin = Boolean.valueOf(msgMap.get("isLogin"));
-
- String json = "success";
- if(!isLogin){
- json = "error";
- }
-
- json = JSONUtil.serialize(json);
- getResponse().setCharacterEncoding("utf-8");
- getResponse().getWriter().write(json);
- return null;
- }
-
- public ILoginService getLoginService() {
- return loginService;
- }
- public void setLoginService(ILoginService loginService) {
- this.loginService = loginService;
- }
-
-
- public Login getLogin() {
- return login;
- }
-
-
- public void setLogin(Login login) {
- this.login = login;
- }
-
- }
DAO接口:
-
-
-
- package com.gzedu.login.dao;
-
- import java.util.Map;
-
- import com.gzedu.login.entity.Login;
-
-
-
-
-
- public interface ILoginDao {
-
-
-
-
-
- public Map<String,String> login(Login login);
- }
DAO实现类:
-
-
-
- package com.gzedu.login.dao.impl;
-
-
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- import org.springframework.jdbc.core.support.JdbcDaoSupport;
-
- import com.gzedu.login.dao.ILoginDao;
- import com.gzedu.login.entity.Login;
-
-
-
-
-
- public class LoginDaoImpl extends JdbcDaoSupport implements ILoginDao{
- @Override
- public Map<String,String> login(Login login) {
- String sql = "select * from t_user where username = ? and password = ?";
-
- List list = getJdbcTemplate().queryForList(sql,new Object[]{login.getUsername(),login.getPassword()});
-
- String isLogin = "true";
- if(list.isEmpty()){
- isLogin = "false";
- }
- Map<String,String> map = new HashMap<String, String>();
- map.put("isLogin", isLogin);
- return map;
- };
- }
Login中间层接口:
-
-
-
- package com.gzedu.login.service;
-
- import java.util.Map;
-
- import com.gzedu.login.entity.Login;
-
-
-
-
-
- public interface ILoginService {
-
-
-
-
-
- public Map<String,String> login(Login login);
- }
中间层的实现类:
-
-
-
- package com.gzedu.login.service.impl;
-
- import java.util.Map;
-
- import com.gzedu.login.dao.ILoginDao;
- import com.gzedu.login.entity.Login;
- import com.gzedu.login.service.ILoginService;
-
-
-
-
-
- public class LoginServiceImpl implements ILoginService{
- private ILoginDao loginDao;
-
- public ILoginDao getLoginDao() {
- return loginDao;
- }
-
- public void setLoginDao(ILoginDao loginDao) {
- this.loginDao = loginDao;
- }
-
- @Override
- public Map<String,String> login(Login login) {
- return loginDao.login(login);
- }
- }
login实体类:、
-
-
-
- package com.gzedu.login.entity;
-
-
-
-
-
- public class Login {
-
-
-
- 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;
- }
- }
applicationContext.xml配置
- <?xml version="1.0" encoding="UTF-8"?>
- <beans
- xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- 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.1.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
-
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="username">
- <value>root</value>
- </property>
- <property name="password">
- <value>root</value>
- </property>
- <property name="driverClassName">
- <value>com.mysql.jdbc.Driver</value>
- </property>
- <property name="url">
- <value>jdbc:mysql://127.0.0.1:3306/jqdb</value>
- </property>
- </bean>
-
-
-
- <bean id="loginDao" class="com.gzedu.login.dao.impl.LoginDaoImpl">
- <property name="dataSource" ref="dataSource" />
- </bean>
- <bean id="loginService" class="com.gzedu.login.service.impl.LoginServiceImpl">
- <property name="loginDao" ref="loginDao" />
- </bean>
-
- </beans>
login的xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <package name="loginpackage" extends="struts-default" namespace="/login">
- <action name="login" method="login" class="com.gzedu.login.action.LoginAction">
- <result name="success">/pages/login/main.jsp</result>
- </action>
- <action name="checkLogin" method="checkLogin" class="com.gzedu.login.action.LoginAction">
- </action>
- </package>
-
- </struts>
struts2 的xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
-
- <struts>
-
- <constant name="struts.devMode" value="true" />
- <constant name="struts.objectFactory" value="spring" />
-
-
- <include file="login.xml"></include>
- </struts>
struts.properties配置
- #该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,
- #即所有匹配*.action的请求都由Struts 2处理.如果用户需要指定多个请求后缀,
- #则多个后缀之间以英文逗号(,)隔开.
- struts.action.extension = do
jsp页面
login.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <%
- String path = request.getContextPath();
- %>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>登录</title>
- <script type="text/javascript" src="<%=path %>/js/jquery/1.7/jquery-1.7.1.min_production.js"></script>
- </head>
- <body>
- <form action="<%=path %>/login/login.do" method="post">
- 用户名:<input type="text" name="login.username" /><br/>
- 密 码:<input type="password" name="login.password" /><br/>
- <input type="button" value="登录" onclick="login();">
- <input type="button" value="取消"><br/>
- <input type="submit" value="提交" style="display: none"><br/>
- <div id="errMsg" style="text-algin:center;color: red"></div>
- </form>
-
- <script type="text/javascript">
- function login(){
- var url = "<%=path %>/login/checkLogin.do";
- $.ajax({
- type : "POST",
- url : url,
- dataType : "json",
- data : {
- "login.username": $("input[name='login.username']").val(),
- "login.password": $("input[name='login.password']").val()
- },
- success : function (data){
- if("error" == data){
- $("#errMsg").html("<p>用户名或者密码错误,请重新输入!</p>");
-
- setTimeout("$('#errMsg').html('')",10000);
- }else{
- $("input[type=submit]").click();
- }
- },
- error : function (jqXHR, textStatus, errorThrown){
- alert(textStatus);
- }
- });
- }
- </script>
- </body>
- </html>
main.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>主页</title>
- </head>
- <body>
- <h2>我是主页,我怕谁!</h2>
- </body>
- </html>
7、运行
正确登录:
分享到:
相关推荐
struts2+spring4+mybatis3,登录jquery ajax,struts拦截器,springAOP的例子。带部分注释。
这是一个完整的S2SI框架,附jar包和建表语句,里面有添、删、改、查通用查询方法,并且,加了log4j,所以对数据库操作SQL都会在控制台打印出来,加有最新的jQuery插件1.7.2.min.js,建好表,部署完工程直接就可以...
本例子,使用struts+spring+hibernate +jquery 实现的jquery的treeview 和 contextmenu 插件的整合,实现了无限级的tree
Struts1.3+Hibernate3.2+Spring2.5+JQuery1.3
用jqueryUI 实现 上下左的布局 数据库及源码都在上传的RAR包中 由于上传空间的问题,JAR包不做上传,大家可以自己添加 spring 2.0.1 ,hibernate3.0,strutd1.2,相关JAR包; 及c3p0-0.9.1.2.jar, mysql-connector-...
easyui的一个例子,我自己写的,肯定好使
Struts Spring hibernate jquery oracle增删改查以及分页,里面含有包,实现局部刷新,我刚到公司时候做的小例子,很清晰,一看就会!
在原来的项目中集成juery的ajax功能,返回json串,结果报了一大堆版本冲突以及jar包缺失的问题,在网上查了老半天资料终于...struts2-spring-plugin-2.1.6.jar json-lib-2.1.jar jsonplugin-0.34.jar xwork-2.1.2.jar
1.前端使用jquery-ztree插件,后端读取数据库,封装成json格式,在前端展示。数据库字段和Tree这个实体类的字段是一致的。struts2+spring架构。 2.另外里面还含有定时启动的例子。Spring-Quartz的配置和使用。
jQuery+json+struts2+spring3(事务,AOP,IoC即DI)+hibernte3+EhCache+oscache+proxool+MySQL+SiteMesh+Gzip lj例子
弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,...
并且已经包含了jquery,json等插件,可直接使用Ajax功能!由于文件大小限制,jar包中删除了hibernate的核心jar,需要大家自己下个添加,其余jar包已经完整!测试例子已经删除。需要手动编写。出错的话自己检查代码。...
和maven官方例子一样,我学习教程也采用了父工程来管理各个阶段尝试...\babasportrefactoring\babasport1-30 为子model1,Spring3.05 + Strtus2 + JPA(Hibernate) + Sitemesh + Jquery(部分测试页面),同步更新到30讲
近期学习了SSH2(Struts2+Spring+Hibernate)的整合后,开始尝试的写一个登陆界面,结果发现:若是单单使用struts2来进行页面跳转的话页面的效果不怎么样,同时也无法进行局部刷新(即异步提交验证)。 于是,我开始...
Java软件开发中,后台中我们可以通过各种框架,像SSH等进行对代码的封装,方便我们对Java代码的编写,例如,Struts,SpringMVC对从前台到action的流程进行封装控制,使我们只需要进行一些简单配置就可以实现;...
基于struts2+spring+springjdbc开发的代码分享网,所有源码已开源。 网站功能介绍: 1、邮件注册(采用阿里云企业邮箱),为了让大家体验一下邮箱注册功能。我已经在分享的源码中,为大家配置好了测试账户,大家可以...
两个例子,无状态SessionBean可会话Bean必须实现SessionBean,获取系统属性,初始化JNDI,取得Home对象的引用,创建EJB对象,计算利息等;在有状态SessionBean中,用累加器,以对话状态存储起来,创建EJB对象,并将...
两个例子,无状态SessionBean可会话Bean必须实现SessionBean,获取系统属性,初始化JNDI,取得Home对象的引用,创建EJB对象,计算利息等;在有状态SessionBean中,用累加器,以对话状态存储起来,创建EJB对象,并将...