Spring+Struts2+JQuery配合实现简单的登录功能

来源:互联网 发布:网络解码器 编辑:程序博客网 时间:2024/05/22 01:29

前几天朋友打电话 ,说他要实现使用Spring+Struts2+JQuery的登录功能,因为现在公司里框架都是固定的,突然让你自己写一个,好久不写写不出来,百度了好久都是各种版本,各种残缺,各种不理想,只好自己去写,希望对其他人有所帮助。故有此博客。本人菜鸟写的不好请各位包涵!

 

前台页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>Login</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><script type="text/javascript" src="js/jquery.js"></script>  </head>    <body>    用户名: <input type="text" id="name"/>    密码:<input type="password" id="password"/>    <input type="submit" value="登陆" onclick="check();"/>  </body> <script type="text/javascript">function check(){  var name=$("#name");var password=$("#password");  $.ajax({  type:'post',  url:'http://localhost:8080/LoginDemo/test/login.action',  dataType:'json',  data:{name:name.val(),password:password.val()},  success:function(data,textStatus){  if(data.success){  alert('登陆成功');  }else  {  alert('登陆失败');  }  },  error:function(data,textStatus){  alert('访问后台失败');  }  });} </script></html>
后台action
package com.login.action;import java.io.PrintWriter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionSupport;import com.login.service.LoginService;public class LoginAction extends ActionSupport implements Action{    private LoginService loginService;public LoginService getLoginService() {        return loginService;    }    public void setLoginService(LoginService loginService) {        this.loginService = loginService;    }    public String login()    {          HttpServletRequest request=ServletActionContext.getRequest();    HttpServletResponse response=ServletActionContext.getResponse();response.setContentType("text/html;charset=utf-8");PrintWriter out = null; try {request.setCharacterEncoding("utf-8");String name=request.getParameter("name");String password=request.getParameter("password");out = response.getWriter();if(loginService.login(name, password)){out.println("{\"success\":true}");}else{out.println("{\"success\":false}");}} catch (Exception e) {e.printStackTrace();}finally{out.flush();out.close();}return null;}} 

service类:

package com.login.service;import com.login.dao.UserDao;public class LoginService {    private UserDao userDao;    public UserDao getUserDao() {return userDao;}public void setUserDao(UserDao userDao) {this.userDao = userDao;}public boolean login(String name, String password) throws Exception{        boolean temp=false;        if(userDao.checkUser(name, password)){        temp=true;        }        return temp;    }  }


dao层:

package com.login.dao;import org.springframework.jdbc.core.JdbcTemplate;public class UserDao {    private JdbcTemplate jdbcTemplate;    //注入    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {        this.jdbcTemplate = jdbcTemplate;    }public boolean checkUser(String username, String password) {        String sql="select count(*) from sys_user where username=? and password=? ";        int count=jdbcTemplate.queryForInt(sql,new Object[]{username,password});        return count>0;    }}



struts2配置文件:

<?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="test"  extends="struts-default">          <action name="login" class="loginAction" method="login"></action>    </package>   </struts>


Spring配置文件:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC       "-//SPRING//DTD BEAN//EN"      "http://www.springframework.org/dtd/spring-beans.dtd"> <beans>    <!-- 数据源配置 -->    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>        <property name="url" value="jdbc:mysql://localhost:3306/mysql"></property>        <property name="username" value="root"></property>        <property name="password" value="root"></property>    </bean>    <!-- 定义Spring JDBC模板类bean -->    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">        <property name="dataSource" ref="dataSource"></property>    </bean>    <!-- dao -->    <bean id="userDao" class="com.login.dao.UserDao">        <property name="jdbcTemplate" ref="jdbcTemplate" ></property>    </bean>    <!-- service -->    <bean id="loginService" class="com.login.service.LoginService">        <property name="userDao" ref="userDao"></property>    </bean>    <!-- action -->    <bean id="loginAction" class="com.login.action.LoginAction">        <property name="loginService" ref="loginService"></property>    </bean></beans>

web.xml:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><!-- 指定Spring的配置文件 默认从web根目录寻找,这里指定从类路径下寻找这样就无关路径了 --><context-param>   <param-name>contextConfigLocation</param-name>   <param-value>classpath:applicationContext.xml</param-value>  </context-param>  <!-- 对spring容器进行实例化 -->  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <!-- 配置Struts2 -->   <filter>    <filter-name>struts2</filter-name>    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  </filter>  <filter-mapping>    <filter-name>struts2</filter-name>    <url-pattern>/*</url-pattern>    </filter-mapping>       <!-- 默认界面配置 --><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>



 

0 0