s2sh整合的登陆邮电部分借鉴过前者的一些经验
多谢指点…………
1.导入jar包,配置web.xml,建实体类写xxx.hbm.xml
entity:
User.java
package entity;
public class User {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String name;
private String password;
}
User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="entity">
<class name="User" table="user">
<id name="id" column="id" type="java.lang.Integer">
<generator class="increment" />
</id>
<property name="name" column="name" type="java.lang.String" />
<property name="password" column="password" type="java.lang.String" />
</class>
</hibernate-mapping>
Mail.java//邮电属性
package entity;
import java.util.Properties;
public class MailSenderInfo {
// 发送邮电的服务器的IP和端口
private String mailServerHost;
public String getMailServerHost() {
return mailServerHost;
}
public void setMailServerHost(String mailServerHost) {
this.mailServerHost = mailServerHost;
}
public String getMailServerPort() {
return mailServerPort;
}
public void setMailServerPort(String mailServerPort) {
this.mailServerPort = mailServerPort;
}
public String getFromAddress() {
return fromAddress;
}
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
public String getToAddress() {
return toAddress;
}
public void setToAddress(String toAddress) {
this.toAddress = toAddress;
}
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 boolean isValidate() {
return validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String[] getAttachFileName() {
return attachFileName;
}
public void setAttachFileName(String[] attachFileName) {
this.attachFileName = attachFileName;
}
private String mailServerPort = "25";
private String fromAddress;
private String toAddress;
// 用户名和密码
private String userName;
private String password;
// 是否验证
private boolean validate = false;
// 主题
private String subject;
// 内容
private String content;
// 邮电附件
private String[] attachFileName;
/**
* 获得邮电会话的属性
*/
public Properties getProperties() {
Properties p = new Properties();
p.put("mail.smtp.host", this.mailServerHost);
p.put("mail.smtp.port", this.mailServerPort);
p.put("mail.smtp.auth", validate ? "true" : "false");
return p;
}
}
dao:
UserDAO
package dao;
import entity.User;
public interface UserDAO {
public User findUserByName(String name);
}
UserDAOimpl
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.orm.hibernate3.HibernateTemplate;
import util.ConnDB;
import entity.User;
public class UserDAOimpl implements UserDAO {
private HibernateTemplate hibernateTemplate;
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public User findUserByName(String name) {
// return (User) this.getHibernateTemplate().find(hql).get(0);
/*
* hibernateTemplate.execute(new HibernateCallback() {
*
* public Object doInHibernate(Session arg0) throws HibernateException,
* SQLException { // TODO Auto-generated method stub String
* hql="from User as u where u.name=?"; Query
* query=arg0.createQuery(hql); query.s return null; } });
*/
String hql = "from User as u where u.name=?";
Object[] obj = { name };
List<User> users = hibernateTemplate.find(hql, obj);
if (users.size() > 0) {
return users.get(0);
} else {
return null;
}
}
public List<User> findAllUser() {
return hibernateTemplate.find("from User");
}
public static void main(String[] args) {
Connection conn = ConnDB.getConnection();
List<User> us = new ArrayList<User>();
try {
PreparedStatement ps = conn.prepareStatement("select * from user");
ResultSet res = ps.executeQuery();
while (res.next()) {
User user = new User();
user.setId(res.getInt(1));
user.setName(res.getString(2));
user.setPassword(res.getString(3));
us.add(user);
}
conn.close();
res.close();
ps.close();
System.out.println(us.size());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// UserDAOimpl udi = new UserDAOimpl();
// // System.out.println(udi.findUserByName("qq"));
// System.out.println(udi.findAllUser().get(0).getName());
}
}
service
UserService
package service;
import entity.MailSenderInfo;
import entity.User;
public interface UserService {
public User findUserByName(String name);
public void sendMail(MailSenderInfo mailInfo);
}
UserServiceimpl
package service;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import util.SimpleMailSender;
import dao.UserDAO;
import entity.MailSenderInfo;
import entity.User;
public class UserServiceimpl implements UserService {
private UserDAO userdao;
public UserDAO getUserdao() {
return userdao;
}
public void setUserdao(UserDAO userdao) {
this.userdao = userdao;
}
public User findUserByName(String name) {
// TODO Auto-generated method stub
return userdao.findUserByName(name);
}
public void sendMail(MailSenderInfo mailInfo) {
// TODO Auto-generated method stub
SimpleMailSender sms = new SimpleMailSender();
sms.sendTextMail(mailInfo);
}
}
action
UserAction
package actin;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import entity.MailSenderInfo;
import entity.User;
public class UserAction extends ActionSupport{
private String vc;
public String getVc() {
return vc;
}
public void setVc(String vc) {
this.vc = vc;
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String password;
private UserService userservice;
public UserService getUserservice() {
return userservice;
}
public void setUserservice(UserService userservice) {
this.userservice = userservice;
}
public String login(){
User user2=userservice.findUserByName(name);
if(user2!=null && user2.getPassword().equals(password)){
MailSenderInfo mailInfo = new MailSenderInfo();
mailInfo.setMailServerHost("smtp.sina.com.cn");
mailInfo.setMailServerPort("25");
mailInfo.setValidate(true);
mailInfo.setUserName(" *****@sina.cn");
mailInfo.setPassword("*");
mailInfo.setFromAddress(" *****@sina.cn");
mailInfo.setToAddress("
[email protected]");
mailInfo.setSubject("登陆信息");
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
mailInfo.setContent("亲爱的"+user2.getName()+"您登陆的时间为:"+sdf.format(new Date())+"\n"+"java运行的环境版本"+System.getProperty("java.version")+"\n当前系统用户名"+System.getProperty("user.name")+"\n操作系统名称"+System.getProperty("os.name"));
userservice.sendMail(mailInfo);
return "succ";
}else{
return "error";
}
}
public String valicode(){
return "valicodesucc";
}
public String Vli(){
HttpSession session=ServletActionContext.getRequest().getSession();
String vcc=(String) session.getAttribute("number");
if(vcc.equals(vc)){
vc="OK";
}else{
vc="NO";
}
return "v_value";
}
}
beans.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-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:ConnDB.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username">
<value>${username}</value>
</property>
<property name="password">
<value>${password}</value>
</property>
<property name="url">
<value>${url}</value>
</property>
<property name="driverClassName">
<value>${driverClassName}</value>
</property>
</bean>
<bean id="sessionfactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=true
hibernate.show_sql=true
hibernate.format_sql=true
</value>
</property>
<property name="mappingResources">
<value>entity/User.hbm.xml</value>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref local="sessionfactory" />
</property>
</bean>
<bean id="userdao" class="dao.UserDAOimpl">
<property name="hibernateTemplate">
<ref local="hibernateTemplate" />
</property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionfactory" />
</property>
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<!--
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host">
<value>smtp.sina.com.cn</value>
</property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
<prop key="mail.smtp.socketFactory.fallback">false</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
<property name="username">
<value>
[email protected]</value>
</property>
<property name="password">
<value>111dan</value>
</property>
<property name="port">
<value>25</value>
</property>
</bean>
-->
<bean id="userservice" class="service.UserServiceimpl">
<property name="userdao">
<ref local="userdao" />
</property>
</bean>
<bean id="userAction" class="actin.UserAction" scope="prototype">
<property name="userservice">
<ref local="userservice" />
</property>
</bean>
</beans>
ConnDB.properties
username=
password=
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test1
struts.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="struts" extends="struts-default">
<result-types>
<result-type name="text" class="util.TextResult"/>
<result-type name="imag" class="util.ValiCode"/>
</result-types>
<action name="u_*" class="userAction" method="{1}">
<result name="valicodesucc" type="imag"></result>
<result name="succ">/index.jsp</result>
<result name="error">/error.jsp</result>
<result name="v_value" type="text"></result>
</action>
</package>
</struts>
UserLogin.jsp
<form action="u_login" method="post" name="Myform">
<fieldset>
<legend>用户登录</legend>
用户:<input type="text" name="name"/>
密码:<input type="password" name="password"/>
验证:<img src="u_valicode" id="num"/>
<span id="number" style="color:red">请输入验证码</span>
<input type="text" name="valicode" id="valicode"/>
<a href="javascript:;" onclick="document.getElementById('num').src='u_valicode?'+(new Date()).getTime()">看不清,换一张</a><!-- 换图片 -->
</fieldset>
<input type="submit" value="登录"/>
</form>
希望高手多多指点………………