首页 > 代码库 > SSH三大框架的理解
SSH三大框架的理解
1、Strus里面写Action相关的东西主要是负责界面的跳转(过滤功能)。
在WEB-INFO的src的struts.xml文件中进行配置
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.custom.i18n.resources"
value=http://www.mamicode.com/"messageResource"/>
<constant name="struts.i18n.encoding" value=http://www.mamicode.com/"GBK"/>
<package name="lee" extends="struts-default">
<!-- 配置系统的拦截器 -->
<interceptors>
<!-- 定义系统的登录控制的拦截器 -->
<interceptor name="authority"
class="org.crazyit.auction.action.interceptor.AuthorityInterceptor"/>
<!-- 定义系统auctionStack拦截器栈 -->
<interceptor-stack name="auctionStack">
<!-- 引用系统的默认拦截器栈 -->
<interceptor-ref name="defaultStack"/>
<!-- 引用登录控制拦截器 -->
<interceptor-ref name="authority"/>
</interceptor-stack>
</interceptors>
<!-- 将自定义的拦截器栈定义成默认拦截器栈 -->
<default-interceptor-ref name="auctionStack"/>
<global-results>
<!-- 下面定义的结果对所有的Action都有效 -->
<result name="login">/WEB-INF/content/login.jsp</result>
<result name="exception">/WEB-INF/content/error.jsp</result>
</global-results>
<global-exception-mappings>
<!-- 指Action抛出AuctionException异常时,转入名为exception的结果。 -->
<exception-mapping exception="org.crazyit.auction.exception.AuctionException" result="exception"/>
<!-- 指Action抛出Exception异常时,转入名为exception的结果。 -->
<exception-mapping exception="java.lang.Exception" result="exception"/>
</global-exception-mappings>
<action name="index">
<result name="success">/WEB-INF/content/main.jsp</result>
</action>
<action name="viewItemSu" class="org.crazyit.auction.action.ViewSuAction">
<result name="success">/WEB-INF/content/viewSuccess.jsp</result>
</action>
<action name="viewFailItem" class="org.crazyit.auction.action.ViewFailAction">
<result name="success">/WEB-INF/content/viewFail.jsp</result>
</action>
<action name="viewBid" class="org.crazyit.auction.action.ViewBidAction">
<result name="success">/WEB-INF/content/viewBid.jsp</result>
</action>
<action name="mgrItem" class="org.crazyit.auction.action.MgrItemAction">
<result name="success">/WEB-INF/content/mgrItem.jsp</result>
</action>
<action name="mgrKind" class="org.crazyit.auction.action.MgrKindAction">
<result name="success">/WEB-INF/content/mgrKind.jsp</result>
</action>
<action name="viewKind" class="org.crazyit.auction.action.ViewKindAction">
<result name="success">/WEB-INF/content/viewKind.jsp</result>
</action>
<action name="viewItem" class="org.crazyit.auction.action.ViewItemAction">
<result name="input">viewKind.action</result>
<result name="success">/WEB-INF/content/viewItem.jsp</result>
</action>
<action name="viewDetail" class="org.crazyit.auction.action.ViewDetailAction">
<result name="error">/WEB-INF/content/error.jsp</result>
<result name="success">/WEB-INF/content/viewDetail.jsp</result>
</action>
<!-- 配置处理用户登录的Action -->
<action name="proLogin" class="org.crazyit.auction.action.LoginAction">
<!-- 配置处理用户登录后的Result -->
<result name="input">/WEB-INF/content/login.jsp</result>
<result name="success">/WEB-INF/content/main.jsp</result>
<result name="failure">/WEB-INF/content/login.jsp</result>
<!-- 显式引用系统默认拦截器栈 -->
<interceptor-ref name="defaultStack"/>
</action>
<action name="proAddItem" class="org.crazyit.auction.action.AddItemAction">
<result name="input" type="chain">mgrItem</result>
<result name="success" type="chain">mgrItem</result>
</action>
<action name="proAddKind" class="org.crazyit.auction.action.AddKindAction">
<result name="input" type="chain">mgrKind</result>
<result name="success" type="chain">mgrKind</result>
</action>
<action name="bid" class="org.crazyit.auction.action.AddBidAction">
<result name="input" type="chain">viewDetail</result>
<result name="success" type="chain">viewBid</result>
</action>
</package>
</struts>
2、Servlet负责客户端和服务端的交互
复写service方法,service方法再通过请求方式进行分发。
3、H主要负责数据持久化操作
3.1对象与数据表对应
@Entity
@Table(name="auction_user")
public class AuctionUser
{
// 标识属性
@Id
@Column(name="user_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
// 用户名属性
private String username;
// 密码属性
private String userpass;
// 电子邮件属性
private String email;
// 根据属主关联的物品实体
@OneToMany(targetEntity=Item.class ,
mappedBy="owner")
private Set<Item> itemsByOwner = new HashSet<Item>();
// 根据赢取者关联的物品实体
@OneToMany(targetEntity=Item.class ,
mappedBy="winer")
private Set<Item> itemsByWiner = new HashSet<Item>();
// 该用户所参与的全部竞价
@OneToMany(targetEntity=Bid.class ,
mappedBy="bidUser")
private Set<Bid> bids = new HashSet<Bid>();
// 无参数的构造器
public AuctionUser()
{
}
// 初始化全部成员变量的构造器
public AuctionUser(Integer id , String username
, String userpass , String email)
{
this.id = id;
this.username = username;
this.userpass = userpass;
this.email = email;
}
// id的setter和getter方法
public void setId(Integer id)
{
this.id = id;
}
public Integer getId()
{
return this.id;
}
// username的setter和getter方法
public void setUsername(String username)
{
this.username = username;
}
public String getUsername()
{
return this.username;
}
// userpass的setter和getter方法
public void setUserpass(String userpass)
{
this.userpass = userpass;
}
public String getUserpass()
{
return this.userpass;
}
// email的setter和getter方法
public void setEmail(String email)
{
this.email = email;
}
public String getEmail()
{
return this.email;
}
// itemsByOwner的setter和getter方法
public void setItemsByOwner(Set<Item> itemsByOwner)
{
this.itemsByOwner = itemsByOwner;
}
public Set<Item> getItemsByOwner()
{
return this.itemsByOwner;
}
// itemsByWiner的setter和getter方法
public void setItemsByWiner(Set<Item> itemsByWiner)
{
this.itemsByWiner = itemsByWiner;
}
public Set<Item> getItemsByWiner()
{
return this.itemsByWiner;
}
// bids的setter和getter方法
public void setBids(Set<Bid> bids)
{
this.bids = bids;
}
public Set<Bid> getBids()
{
return this.bids;
}
}
public interface AuctionUserDao extends BaseDao<AuctionUser>
{
/**
* 根据用户名,密码查找用户
* @param username 查询所需的用户名
* @param pass 查询所需的密码
* @return 指定用户名、密码对应的用户
*/
AuctionUser findUserByNameAndPass(String username , String pass);
}
public class AuctionUserDaoHibernate
extends BaseDaoHibernate4<AuctionUser> implements AuctionUserDao
{
/**
* 根据用户名,密码查找用户
* @param username 查询所需的用户名
* @param pass 查询所需的密码
* @return 指定用户名、密码对应的用户
*/
public AuctionUser findUserByNameAndPass(String username , String pass)
{
// 执行HQL查询
List<AuctionUser> ul = (List<AuctionUser>)find(
"from AuctionUser au where au.username=?0 and au.userpass=?1" ,
username , pass);
// 返回查询得到的第一个AuctionUser对象
if (ul.size() == 1)
{
return (AuctionUser)ul.get(0);
}
return null;
}
}
SSH三大框架的理解