1. 在Ecplise下创建Dynamic Web Project工程:
2. 导入Struts2框架。
工具栏上的new->other->web下
导入成功后在工程中的lib里有相应的JAR包还有以下图标:
这样Struts2框架就导入成功.
3. 导入spring框架:
右击项目选spring tool点击添加即可完成.
4. 加入spring框架需要的Jar包:
spring.jar,cglib-nodep-2.1_3.jar二个包到lib下.
5. 图形化设计struts视图
双击ssh.aswp可得下图,在这里我们可以自由的设计视图:
6. alveole-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>
<!--
WARNING: This file was automatically generated from Eclipse Alveole Studio Plugin.
Do not edit it mannually: it will be erased automatically !
-->
<package extends="alveole-defaults" name="alveole" namespace="/">
<action class="com.echo.usermanager.LoginAction" method="login" name="login">
<result name="success" type="dispatcher">
/main.jsp
</result>
<result name="failure" type="dispatcher">
/error.jsp
</result>
</action>
</package>
</struts>
以上这一个文件不需要修改,由其自动生成。
7。添加spring的applicationConfig.xml。
工具栏上的new->other->spring下:
applicationConfig.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="loginAction" class="com.echo.usermanager.LoginAction">
<property name="userDAO">
<ref bean="userDAO"></ref>
</property>
</bean>
<bean id="userDAO" class="com.echo.dao.UserDAO" singleton="true"></bean>
</beans>
8。public class BaseAction extends ActionSupport {
public final Object getBean(String name) {
WebApplicationContext ctx =
WebApplicationContextUtils.getWebApplicationContext(
ServletActionContext.getServletContext());
return ctx.getBean(name);
}
}
9。
/**
*
*/
package com.echo.usermanager;
import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.echo.dao.UserDAO;
import com.eco.comm.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author Administrator
*
*/
public class LoginAction extends BaseAction {
/**
* Struts action implementation for node 登陆处理
* @return a link to follow
*/
private String userName;
private String password;
private UserDAO userDAO;
/**
* @param userName the userName to set
*/
public final void setUserName(String userName) {
this.userName = userName;
}
/**
* @param password the password to set
*/
public final void setPassword(String password) {
this.password = password;
}
public String login() {
Object bject = getBean("userDAO");
if (object instanceof UserDAO) {
userDAO = (UserDAO)object;
userDAO.name();
}
ActionContext.getContext().getSession().
put("userName", getUserName());
return "success";
}
.................
/**
* @param userDAO the userDAO to set
*/
public final void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
}
10. index.jsp
<form. action="login.action" method="POST">
<div>
<div align="left">
<span>用户名:</span><input type="text" name="userName" value="请输入用户名">
</div>
<div>
<span>密 码:</span><input type="text" name="password" value="请输入用户名">
</div>
</div>
<input type="submit" value="提交">
</form>