就好像騎自行車去遠行,最開始的時候總是激情澎湃,到了路上越走越發現只是在苦苦的支撐,不過等你走完全程,看那些凝固在照片中的風景與記憶,你只有一種感覺,滿足。
今天我們的任務是要把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>
如果你輸入郵箱和密碼,點擊登錄看到“登錄成功”,那麼恭喜你