继上一节《Struts2 + Spring + Hibernate 入门1》了解SSH的使用领域和意义后,接下来搭建一个SSH框架项目来观察各个部件的运行原理。
一、搞个helloworld
推荐用eclipse加插件方式来解决,可以参考这个例子:Eclipse搭建SSH(Struts2+Spring3+Hibernate3)框架项目教程
整合三个框架的过程难点有几个:
1、导入比较多的jar包,容易出现版本不兼容或包不齐全的问题,要避免这个问题发生可以用MyEclipse代替eclipse,具体做法参考这个例子:Struts + Spring + Hibernate基础框架搭建;
package org.ssh.action;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.ssh.pojo.UserInfo;
import org.ssh.service.UserManagerService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 用户动作
* */
public class UserManagerAct extends ActionSupport {
private static final long serialVersionUID = -281275682819237996L;
private UserInfo user = new UserInfo();
private UserManagerService<UserInfo> userService;
private List<UserInfo> users;
private String searchText;
public String doLogin(){
if(this.user.getUname() == null || this.user.getPassword() == null) return INPUT;
//之前这里出了很大问题,解决问题参考:http://blog.csdn.net/xuqianghit/article/details/5697463
try {
UserInfo user = userService.doLogin(this.user.getUname(), this.user.getPassword());
if(user != null){
ActionContext.getContext().getSession().put("userinfo", user);
return doQuery();
}else
return INPUT;
} catch (Exception e) {
return ERROR;
}
}
public String doQuery(){
searchText = getParam("queryText");
users = userService.queryUsers(searchText,UserInfo.class);
return SUCCESS;
}
public String doAdd(){
String result = "";
try {
String param = getParam("param");
if(Integer.parseInt(param) > 0){
user.setId(1);
userService.addUser(user);
result = doQuery();
}else
result = "addUser";
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public String doEdit(){
try {
Integer param = Integer.parseInt(getParam("param"));
if(param == 0){
Integer id = Integer.parseInt(getParam("id"));
user = userService.getUser(UserInfo.class, id);
return "editUser";
}else if(param == 1){
userService.modifyUser(user);
}
} catch (Exception e) {
e.printStackTrace();
}
return doQuery();
}
public String doDelete(){
try {
Integer param = Integer.parseInt(getParam("id"));
userService.deleteUser(param,UserInfo.class);
} catch (Exception e) {
e.printStackTrace();
}
return doQuery();
}
protected String getParam(String key){
return ServletActionContext.getRequest().getParameter(key);
}
public UserManagerService<UserInfo> getUserService() {
return userService;
}
public void setUserService(UserManagerService<UserInfo> userService) {
this.userService = userService;
}
public List<UserInfo> getUsers() {
return users;
}
public UserInfo getUser() {
return user;
}
public void setUser(UserInfo user) {
this.user = user;
}
public String getSearchText() {
return searchText;
}
public void setSearchText(String searchText) {
this.searchText = searchText;
}
public String doFuck(){
return "fuckUser";
}
}
期间debug发生了Hibernate Class Is Not Mapped的问题,主要是对hibernate不熟悉导致的,解决方法可以如此:Hibernate Class Is Not Mapped ?
二、观察SSH
完成上面步骤后,一个helloworld级别的SSH项目算是搭成了,可以往里加点内容观察各个部件如何运转。
1、在struts.xml中加入下面代码:
<action name="fuck" class="userAct" method="doFuck">
<result name="fuckUser">/jsp/fuck.jsp</result>
</action>
2、在UserManagerAct.java中加入下面代码:
public String doFuck(){
return "fuckUser";
}
3、在/simpleSSH/WebRoot/jsp中加入fuck.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>My JSP 'fuck.jsp' starting page</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is a fucking page. <br>
<script type="text/javascript">
setTimeout(reDo, 10000);
function reDo(){
window.location.href = "/simpleSSH/index.jsp";
}
</script>
</body>
</html>
4、修改success.jsp如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
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>user list page</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">
</head>
<body>
<form action="query.action" method="post">
<table align="center">
<tr>
<td style="vertical-align: middle;">USERNAME
<input type="text" style="width: 300px;height: 30px;font-size: 16px" name="queryText" value="${searchText }"/>
<input type="submit" value="Query" style="width: 100px;height: 30px;font-size: 16px"/>
<input type="button" value="Add" style="width: 100px;height: 30px;font-size: 16px"
οnclick="window.location.href='add.action?param=0'"/>
<input type="button" value="Fuck" style="width: 100px;height: 30px;font-size: 16px"
οnclick="window.location.href='fuck.action'"/>
</td>
</tr>
</table>
</form>
<table align="center" border="1" cellpadding="0" cellspacing="0" bordercolor="#3366cc">
<tr align="center" bgcolor="#3399cc" height="26px">
<td width="100">NO.</td>
<td width="160">UNAME</td>
<td width="70">SEX</td>
<td width="150">TELPHONE</td>
<td width="300">ADDRESS</td>
<td width="120">EDIT/DELETE</td>
</tr>
<c:forEach var="user" items="${users }">
<tr align="center" height="24px">
<td width="100">${user.id}</td>
<td width="160">${user.uname}</td>
<td width="70">${user.sex }</td>
<td width="150">${user.telphone }</td>
<td width="300">${user.address}</td>
<td width="120">
<a href="edit.action?param=0&id=${user.id}">编辑</a>
<a href="delete.action?id=${user.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
这时点击Fuck按键
就能看到
三、小结
目前为止,可以观察到整个SSH项目的运行大致是怎么样的了:
1、web页面的跳转由struts.xml的配置来确认。比如点击Fuck按键,success.jsp中fuck按键的html代码onclick响应,当前页面转向fuck.action,被Struts2的filter拦截(详见web.xml),Struts2框架响应将处理交给业务逻辑层的doFuck方法处理,获得result,然后跳转到fuck.jsp页面;
2、struts.xml中的class="*"与applicationContext.xml中的bean id="*"对应;
3、struts.xml中的action name="*"与jsp中的action="*.action"对应;
4、从model层到持久层到数据库的调用关系就像例子中的applicationContext.xml
<bean id="userAct" class="org.ssh.action.UserManagerAct" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
<bean id="userService" class="org.ssh.service.UserManagerService">
<property name="dao" ref="dao"></property>
</bean>
<bean id="dao" class="org.ssh.dao.BaseDAO">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
此节完毕,
待续。。。。。。