eclipse struts2+spring+H开发

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>

 

未命名
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值