----------------------------------------------------DAO-----------------------------------------------------------
package com.jb.jubmis.Dao.DaoImpl;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import org.springframework.transaction.annotation.Transactional;
import sun.security.provider.MD5;
import com.jb.jubmis.Dao.PubUserDao;
import com.jb.jubmis.entity.Pubuser;
import com.jb.jubmis.entity.Xskh;
public class PubUserDaoImpl extends JpaDaoSupport implements PubUserDao {
@SuppressWarnings("unchecked")
public List<Pubuser> login(final Pubuser pubuser) {
try {
//List<Pubuser> user=super.getJpaTemplate().find(" select u from Pubuser u where u.acode='"+pubuser.getAcode()+"'");
List ret = (List) this.getJpaTemplate().execute(
new JpaCallback() {
public Object doInJpa(EntityManager em){
Query query = null;
query = em.createQuery(" select u from Pubuser u where u.password=:p and u.status=0 and (u.account=:a or u.code=:a) ");
query.setParameter("a", pubuser.getAcode());
query.setParameter("p",pubuser.getPass());
return query.getResultList();
}
});
return ret;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//获取最大序号
@SuppressWarnings("unchecked")
public Integer getMaxPxxh() {
Integer pxxh=0;
try {
List<Pubuser> xslist=super.getJpaTemplate().find(" select x from Pubuser x order by x.serialNum desc ");
if(xslist.size()>0){
pxxh=xslist.get(0).getPxxh();
}
} catch (Exception e) {
System.out.println("获取最大序号出错!"+e.getMessage());
}
return pxxh;
}
public Pubuser register(Pubuser pubuser) {
try {
super.getJpaTemplate().persist(pubuser);
return pubuser;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@SuppressWarnings("unchecked")
public List<Pubuser> checkInfo(String sql) {
try {
return super.getJpaTemplate().find(sql);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
----------------------------------------------------services-----------------------------------------------------------
package com.jb.jubmis.Services.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
import com.jb.jubmis.Dao.PubUserDao;
import com.jb.jubmis.Services.PubUserServices;
import com.jb.jubmis.comm.Comm;
import com.jb.jubmis.entity.Pubuser;
public class PubUserServicesImpl implements PubUserServices {
private PubUserDao pubUserDao;
// 登
public List<Pubuser> saveInfo(Pubuser user) {
List<Pubuser> list=new ArrayList<Pubuser>();
try {
// 登录
user.setPass(Comm.encode(user.getPass()));
list = pubUserDao.login(user);
return list;
} catch (Exception e) {
return list;
}
}
// 注册
@Transactional
public Pubuser saveRegister(Pubuser user) {
Pubuser pu = new Pubuser();
try {
// 注册
user.setAguid(Comm.getAguid());
user.setDateIn(Comm.getDate());
user.setPxxh(getMaxPxxh());
user.setPass(Comm.encode(user.getPass()));//MD5 PWD
pu = pubUserDao.register(user);
return user;
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}
//获取最大序号
public Integer getMaxPxxh(){
return this.pubUserDao.getMaxPxxh()+1;
}
//帐号是否存在
public List<Pubuser> checkInfo(Pubuser user) {
List<Pubuser> list=new ArrayList<Pubuser>();
try {
list = pubUserDao.checkInfo("select u from Pubuser u where u.code='"+user.getAcode()+"'");
return list;
} catch (Exception e) {
return list;
}
}
public void setPubUserDao(PubUserDao pubUserDao) {
this.pubUserDao = pubUserDao;
}
}
----------------------------------------------------action-----------------------------------------------------------
package com.jb.jubmis.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import com.google.gson.reflect.TypeToken;
import com.jb.jubmis.Services.ExcelServices;
import com.jb.jubmis.Services.PubUserServices;
import com.jb.jubmis.comm.Comm;
import com.jb.jubmis.entity.Pubuser;
import com.jb.jubmis.entity.Xskh;
public class PubuserAction extends MultiActionController {
private PubUserServices pubUserServices;
private ExcelServices excelServices;
private String indexPage;
private String loginPage;
private String registerPage;
private Integer pageNo=1;
private Integer pageSize=15;
private Pubuser user =new Pubuser();
public ModelAndView login(HttpServletRequest request, HttpServletResponse response){
Map map=new HashMap();
List<Xskh> xskh=new ArrayList<Xskh>();
try {
String acode=request.getParameter("acode");
String pass=request.getParameter("pass");
user.setAcode(acode);
user.setPass(pass);
List<Pubuser> us=pubUserServices.saveInfo(user);//登录
if(us.size()>0){
Map<Object, Object> mapinf=new HashMap<Object, Object>();
mapinf.put("aguid", us.get(0).getAguid());
mapinf.put("pageNo", -1);//查询所有
mapinf.put("pageSize",pageSize);
//计算分页
int size=excelServices.getInfo(mapinf).size();
request.setAttribute("size",size);
if(size==0){
size=1;
}
int pageAll=size/pageSize;//计算总页数
if(size%pageSize!=0){
pageAll++;
}
//分页查询
mapinf.remove("pageNo");
mapinf.put("pageNo", pageNo);
mapinf.put("pageSize",pageSize);
xskh=excelServices.getAll(mapinf);
request.setAttribute("pageNo",pageNo);//当前页
request.setAttribute("pageAll",pageAll);//末页
map.put("xskh", xskh);
request.getSession().setAttribute("user",us.get(0));
return new ModelAndView(getIndexPage(),map);
}else{
request.setAttribute("msg", "抱歉,帐号或密码不对!");
return new ModelAndView(getLoginPage(),map);
}
} catch (Exception e) {
//e.printStackTrace();
System.err.println("登录或注册:"+e.getMessage());
//失败
request.setAttribute("msg", "抱歉,帐号或密码不对!");
return new ModelAndView(getLoginPage(),map);
}
}
}
----------------------------------------------------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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"
xmlns:tx="http://www.springframework.org/schema/tx">
<!-- 自动搜索@Controller标注的类用于指明系统从哪个路径下寻找controller,然后提前初始化这些对象。-->
<!-- <context:component-scan base-package="com.jb.jubmis.action" /> -->
<!-- <context:component-scan base-package="com.comingx.wf.controller" /> -->
<!--上传控制-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="utf-8">
<property name="maxUploadSize">
<value>104857600</value>
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
</bean>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<!--接受请求转入控制器,jsp调用的路径 -->
<prop key="excel.do">excelAction</prop>
<prop key="upload.do">uploadAction</prop>
<prop key="pubuser.do">pubuserAction</prop>
</props>
</property>
</bean>
<!--定义视图 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.InternalResourceView
</value>
</property>
<!--所有的jsp页面都放在根目录的jsp文件夹中 -->
<!--
<property name="prefix">
<value>/jsp/</value>
</property> -->
<!--所有jsp的后缀都是.jsp -->
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!--多动作解析 -->
<bean id="paraMethodResolver"
class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName">
<value>method</value>
</property>
</bean>
<!--dao -->
<bean id="excelDao" class="com.jb.jubmis.Dao.DaoImpl.ExcelDaoImpl"><!-- 访问类 -->
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="pubUserDao" class="com.jb.jubmis.Dao.DaoImpl.PubUserDaoImpl"><!-- 访问类 -->
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- services -->
<bean id="excelServices" class="com.jb.jubmis.Services.impl.ExcelServicesImpl"><!-- 访问类 -->
<property name="excelDao" ref="excelDao" />
</bean>
<bean id="pubUserServices" class="com.jb.jubmis.Services.impl.PubUserServicesImpl"><!-- 访问类 -->
<property name="pubUserDao" ref="pubUserDao" />
</bean>
<!--action-->
<bean id="excelAction" class="com.jb.jubmis.action.ExcelAction"><!-- 访问类 -->
<property name="excelServices" ref="excelServices" />
<property name="methodNameResolver" ref="paraMethodResolver"/>
<!--定义要返回的页面 -->
<property name="viewPage">
<value>index</value>
</property>
<!--定义要返回的页面 -->
<property name="viewExcelPage">
<value>index</value>
</property>
<!--定义要返回的页面 -->
<property name="successPage">
<value>success</value>
</property>
</bean>
<!--pubuserAction-->
<bean id="pubuserAction" class="com.jb.jubmis.action.PubuserAction"><!-- 自己写的类 -->
<property name="pubUserServices" ref="pubUserServices" />
<property name="excelServices" ref="excelServices" />
<property name="methodNameResolver" ref="paraMethodResolver"/>
<!--定义要返回的页面 -->
<property name="indexPage">
<value>index</value>
</property>
<!--定义要返回的页面 -->
<property name="loginPage">
<value>login</value>
</property>
<!--定义要返回的页面 -->
<property name="registerPage">
<value>login</value>
</property>
</bean>
<bean id="uploadAction" class="com.jb.jubmis.action.UploadFileAction"><!-- 访问类 -->
<!--定义要返回的页面 -->
<property name="viewPage">
<value>input</value>
</property>
</bean>
<!--定义事务处理类 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="jubmis" />
<property name="jpaProperties">
<props></props>
</property>
</bean>
<!-- <bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="jubmis" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory"
ref="entityManagerFactory" />
</bean>
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager" />
</bean>
事物管理
<tx:annotation-driven transaction-manager="transactionManager" /> -->
<!--dao -->
<!-- 事物通知 -->
<!-- <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save.*" propagation="REQUIRED"/>
<tx:method name="in.*" propagation="REQUIRED"/>
<tx:method name="delete.*" propagation="REQUIRED"/>
<tx:method name="get.*" read-only="true"/>
<tx:method name="find.*" read-only="true"/>
<tx:method name="query.*" read-only="true"/>
</tx:attributes>
</tx:advice>
注入
<aop:config>
<aop:pointcut expression="execution(* com.jb.jubmis.Services.impl.*.*(..))" id="mycut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="mycut"/>
</aop:config> -->
</beans>
-----------------------------------------------------------persistence.xml----------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="jubmis" transaction-type="RESOURCE_LOCAL">
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
<!-- 实体 -->
<class>com.jb.jubmis.entity.Xskh</class>
<class>com.jb.jubmis.entity.Pubuser</class>
<!-- 连接数据库 -->
<properties>
<property name = "toplink.jdbc.driver" value = "com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name = "toplink.jdbc.url" value = "jdbc:sqlserver://svr:2433;DatabaseName=eqmis"/>
<property name = "toplink.jdbc.user" value = "sa"/>
<property name = "toplink.jdbc.password" value = "1.0"/>
</properties>
</persistence-unit>
</persistence>
-----------------------------------------------------------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/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>500</error-code>
<location>/login.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/login.jsp</location>
</error-page>
<error-page>
<exception-type>com.microsoft.sqlserver.jdbc.SQLServerException</exception-type>
<location>/login.jsp</location>
</error-page>
<servlet>
<servlet-name>dispather</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--applicationContext.xml的路径在src下的META-INF文件夹下-->
<param-value>/WEB-INF/classes/META-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispather</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
----------------------------------------------------jsp---------------------------------------------------------
<form method="post" id="frmlogin" action="pubuser.do?method=login">
<p class="login_fm">
<label>帐 号:</label>
<input type="text" value="" name="acode" id="loginame">
</p>
<p class="login_fm">
<label>密 码:</label>
<input type="password" name="pass" id="loginpwd">
</p><br/><br/>
<p>
<img src="images/btn_login.jpg" alt="提交" onclick="settiemoutlogin('login')"/>
<img src="images/clear.jpg" alt="清空" onclick="clearlogin('loginame','loginpwd')"/>
</p>
</form>