就好像骑自行车去远行,最开始的时候总是激情澎湃,到了路上越走越发现只是在苦苦的支撑,不过等你走完全程,看那些凝固在照片中的风景与记忆,你只有一种感觉,满足。
SSH笔记目录
SSH笔记一 用maven构建项目(源码下载ssim1.rar)
SSH笔记二 整合hibernate和spring(源码下载ssim2-maven.rar)
SSH笔记三 反向生成DAO 优化开发目录(源码下载ssim3-dao.rar)
SSH笔记四 整合struts2(源码下载ssim4-struts2.rar)
SSH笔记五 整合Tiles2(源码下载ssim5-tiles2.rar)
今天我们的任务是要把struts2整合到项目之中,当然先看一下完成之后我们的项目是什么样子,如图
1. 在pom.xml中添加对struts2的依赖,
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.selitech.ssim</groupId>
<artifactId>ssim</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>SSIM Maven Webapp</name>
<description> Web application</description>
<build>
<finalName>${artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webappDirectory>${basedir}/src/main/webapp</webappDirectory>
<warSourceDirectory>${basedir}/src/main/webapp</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.0.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.0.1B</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- Test Case 依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
<scope>test</scope>
</dependency>
<!-- spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-dao</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
</dependency>
<!-- mysql依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.0</version>
</dependency>
<!-- hibernate依赖 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.6.ga</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.1_3</version>
</dependency>
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<version>2.7.6</version>
</dependency>
<!-- struts2依赖 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.1.6</version>
</dependency>
</dependencies>
</project>
2. 配置web.xml,添加struts2的过滤器
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/application-context.xml</param-value>
</context-param>
<!--struts过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- 加载spring配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 设置欢迎页 -->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
3. 添加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>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.objectFactory" value="spring" />
<include file="struts/user.xml"></include>
</struts>
4. 配置 user.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="user" namespace="/user" extends="struts-default">
<action name="login" class="userAction" method="login">
<result name="input">/login.jsp</result>
<result name="success" type="redirect">/welcome.jsp</result>
</action>
</package>
</struts>
5. 添加UserAction和UserManager类
package net.selitech.ssim.web.user;
import java.util.Map;
import net.selitech.ssim.dao.SsimUser;
import net.selitech.ssim.user.UserManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author: juxuejian
* @filename: UserAction.java
* @date: Aug 10, 2009 , 11:10:47 PM
* @description :
*/
public class UserAction extends ActionSupport{
SsimUser user;
UserManager userManager;
public String login(){
if(getSession("loginedUser")!=null) return SUCCESS;
try {
if (getUserManager().checkLogin(user.getUserEmail(), user.getUserPassword())) {
// 更新登录次数
user = getUserManager().findByEmail(user.getUserEmail());
// 修正手工插入数据后导致int为null的问题
// 将登录用户信息存入 session
setSession("username", user.getUserRealname());
setSession("loginedUser", user);
return SUCCESS;
} else {
}
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return INPUT;
}
public SsimUser getSessionLoginedUser() {
SsimUser currentUser = (SsimUser) getSession("loginedUser");
return currentUser;
}
/**
* 读取 session 中的属性值
* @param name
* @return
*/
public static Object getSession(String name) {
ActionContext ctx = ActionContext.getContext();
Map session = ctx.getSession();
return session.get(name);
}
/**
* 向 session 设置属性值
* @param name
* @param value
*/
public static void setSession(Object name, Object value) {
ActionContext ctx = ActionContext.getContext();
Map session = ctx.getSession();
session.put(name, value);
}
public UserManager getUserManager() {
return userManager;
}
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
}
package net.selitech.ssim.user;
import java.util.List;
import net.selitech.ssim.dao.SsimUser;
import net.selitech.ssim.dao.SsimUserDAO;
/**
* @author: juxuejian
* @filename: UserManager.java
* @date: Aug 10, 2009 , 11:10:29 PM
* @description :
*/
public class UserManager {
SsimUserDAO ssimUserDAO;
public boolean checkLogin(String userEmail,String password) throws Exception{
// 是否存在给定用户
SsimUser user = findByEmail(userEmail);
if (user == null) {
//用户不存在
return false;
}
if(user != null && user.getUserPassword() != null && user.getUserPassword().equals(password)) {
return true;
}
return false;
}
@SuppressWarnings("unchecked")
public SsimUser findByEmail(String userEmail){
List<SsimUser> users=ssimUserDAO.findByUserEmail(userEmail);
if(users != null && users.size() > 0) {
return users.get(0);
}
return null;
}
public SsimUserDAO getSsimUserDAO() {
return ssimUserDAO;
}
public void setSsimUserDAO(SsimUserDAO ssimUserDAO) {
this.ssimUserDAO = ssimUserDAO;
}
}
6. 在Spring的配置文件application-conten.xml中注册action和manager
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<import resource="beans/data-source.xml" />
<import resource="beans/ssim-dao.xml" />
<import resource="beans/ssim-user.xml" />
<bean id="configurator" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/ssimdb-config.properties</value>
</list>
</property>
</bean>
</beans>
ssim-user.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="userManager" class="net.selitech.ssim.user.UserManager">
<property name="ssimUserDAO">
<ref bean="SsimUserDAO" />
</property>
</bean>
<bean id="userAction" class="net.selitech.ssim.web.user.UserAction">
<property name="userManager">
<ref bean="userManager"/>
</property>
</bean>
</beans>
7. 添加login.jsp和 welcome.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<title>SSIM</title>
<SCRIPT language=javascript>
function check() {
var s = "";
var n = document.getElementById("user.userEmail").value ? null : s+= "用户名不能为空\n";
var p = document.getElementById("user.userPassword").value ? null : s+="密码不能为空\n";
if(n || p){
alert(s);
return false;
}else{
document.userLoginForm.submit();
}
}
</SCRIPT>
</head>
<body>
<form name="userLoginForm" action="<%=path %>/user/login.action" method=post>
<table>
<tr>
<td>用户名:<input id="user.userEmail" type="text" name="user.userEmail" size="15"/></td></tr>
<tr><td>密 码:<input id="user.userPassword" type="password" name="user.userPassword" size="16"/></td></tr>
<tr><td><input type="submit" value="登录" onclick="return check();"/></td></tr>
</table>
</form>
</body>
</html>
welcome.jsp
<html>
<body>
<h2>登录成功</h2>
</body>
</html>
如果你输入邮箱和密码,点击登录看到“登录成功”,那么恭喜你
今天到此为止,下一次 SSH笔记五 整合Tiles2
posted on 2009-08-11 00:29
ju 阅读(2764)
评论(1) 编辑 收藏 所属分类:
SSH系列