首选创建web.xml 主要是配置struts的actionservlet和spring的字符过滤器
http://java.sun.com/xml/ns/j2ee" xmlns:xsi="" version="2.4" xsi:schemalocation=" ">
characterencodingfilter
org.springframework.web.filter.characterencodingfilter
encoding
utf-8
forceencoding
true
characterencodingfilter
/*
action
org.apache.struts.action.actionservlet
config
/web-inf/struts/struts.xml
debug
3
detail
3
0
action
*.do
10
index.jsp
index.html
然后创建struts.xml(string配置文件) 要注意这里集成了spring插件,把全部spring配置文件注入到contextloaderplugin中
http://struts.apache.org/dtds/struts-config_1_2.dtd">
name="loginvo"
type="org.springframework.web.struts.delegatingactionproxy"
parameter="action"
scope="request">
配置spring配置文件
http://www.springframework.org/dtd/spring-beans.dtd">
classpath:hibernate.properties
com/po/login.hbm.xml
${hibernate.dialect}
${hibernate.showsql}
propagation_required,-exception
propagation_required,-exception
propagation_required,-exception
propagation_required,-exception
propagation_required,-exception
propagation_required,-exception
propagation_required,readonly
propagation_required,readonly
propagation_required,readonly
propagation_required,readonly
propagation_required
配置action将service注入到action
http://www.springframework.org/dtd/spring-beans.dtd">
配置service将dao注入到service
http://www.springframework.org/dtd/spring-beans.dtd">
配置dao 注入session工厂
http://www.springframework.org/dtd/spring-beans.dtd">
数据源属性文件(注意不是hibernate的配置文件,是为了让spring读入的)
hibernate.dialect=org.hibernate.dialect.sqlserverdialect
hibernate.driverclassname=com.mysql.jdbc.driver
hibernate.url=jdbc:mysql://127.0.0.1:3306/ssh
hibernate.username=root
hibernate.password=5719
hibernate.showsql=true
hibernate.maxactive=50
hibernate.maxidle=30
hibernate.maxwait=1000
log4j配置文件(简单)
log4j.rootlogger=error,console,file
log4j.appender.console=org.apache.log4j.consoleappender
log4j.appender.console.layout=org.apache.log4j.patternlayout
log4j.appender.console.layout.conversionpattern=%-5p %d [%f,%l] - %m%n
log4j.appender.file=org.apache.log4j.rollingfileappender
log4j.appender.file.file=f:\\ssh.log
#log4j.appender.file.maxfilesize=100000kb
#log4j.appender.file.maxbackupindex=1
log4j.appender.file.layout=org.apache.log4j.patternlayout
log4j.appender.file.layout.conversionpattern=%-5p %d [%f,%l] - %m%n
下面是类文件
package com.action;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.apache.commons.logging.log;
import org.apache.commons.logging.logfactory;
import org.apache.struts.action.actionform;
import org.apache.struts.action.actionforward;
import org.apache.struts.action.actionmapping;
import org.apache.struts.actions.dispatchaction;
import com.service.loginservice;
import com.vo.loginvo;
public class loginaction extends dispatchaction {
private log logger = logfactory.getlog(loginaction.class);
private loginservice loginservice;
public void setloginservice(loginservice loginservice) {
this.loginservice = loginservice;
}
public actionforward login(actionmapping mapping,actionform form,httpservletrequest request,httpservletresponse response) {
try {
loginvo loginvo = (loginvo) form;
string username = loginvo.getusername();
string password = loginvo.getpassword();
system.out.println(username password);
if(loginservice.validate(username, password)) {
return mapping.findforward("index");
}
return mapping.findforward("error");
} catch (exception e) {
logger.error(e);
return mapping.findforward("error");
}
}
public actionforward save(actionmapping mapping,actionform form,httpservletrequest request,httpservletresponse response) {
try {
loginvo loginvo = (loginvo) form;
loginservice.saveuser(loginvo);
return mapping.findforward("index");
} catch (exception e) {
logger.error(e);
return mapping.findforward("error");
}
}
}
package com.dao;
import java.util.list;
import org.springframework.orm.hibernate3.support.hibernatedaosupport;
import com.po.loginpo;
public class logindao extends hibernatedaosupport {
@suppresswarnings("unchecked")
public string getpassword(string username) {
string hql = "from loginpo l where l.username=?";
list list = getsession().createquery(hql).setstring(0,username).list();
if(list!=null && list.size()>0) {
loginpo loginpo = (loginpo) list.get(0);
return loginpo.getpassword();
}
return null;
}
public void save(loginpo loginpo) {
getsession().save(loginpo);
}
}
package com.po;
import java.io.serializable;
public class loginpo implements serializable {
private static final long serialversionuid = 1l;
private integer id = null;
private string username = null;
private string password = null;
public integer getid() {
return id;
}
public string getpassword() {
return password;
}
public string getusername() {
return username;
}
public void setid(integer id) {
this.id = id;
}
public void setpassword(string password) {
this.password = password;
}
public void setusername(string username) {
this.username = username;
}
}
package com.service;
import com.dao.logindao;
import com.po.loginpo;
import com.vo.loginvo;
public class loginservice {
private logindao logindao;
public void setlogindao(logindao logindao) {
this.logindao = logindao;
}
public boolean validate(string username,string password) {
string pass = logindao.getpassword(username);
if(pass!=null) {
if(pass.equals(password)) {
return true;
}
}
return false;
}
public void saveuser(loginvo loginvo) {
loginpo loginpo = new loginpo();
loginpo.setusername(loginvo.getusername());
loginpo.setpassword(loginvo.getpassword());
system.out.println(loginvo.getusername() "-" loginvo.getpassword() ":save succeed...");
logindao.save(loginpo);
//故意制造异常,测试事务。
//logindao.save(null);
}
}
package com.vo;
import org.apache.struts.action.actionform;
public class loginvo extends actionform {
private static final long serialversionuid = 1l;
private string username = null;
private string password = null;
public string getpassword() {
return password;
}
public string getusername() {
return username;
}
public void setpassword(string password) {
this.password = password;
}
public void setusername(string username) {
this.username = username;
}
}
hibernate映射文件
"">
jsp页面文件
<%@ page contenttype="text/html;charset=utf-8" language="java" %>
username:
password:
username:
password:
jar包太多太大了这里就不发了.具体什么意思都有注视,完全实例,理论知识以后在说,欢迎拍砖嘎嘎...
posted on 2007-08-28 16:17
我心依旧 阅读(25619)
评论(21) 编辑 收藏