搭建纯SSH框架(二)

上一篇文章中,写到了SSH中String和Hibernate的结合,这篇中,我们学习String和Struts的结合。


我们做一整套的增删改查,我们的jsp界面和Action图是这样的:



从图中,我们可以看到,删除是不需要界面的,为3个jsp界面,为添加、修改和列表,一共有6个Action:

1、在列表界面中点击添加,会弹出添加界面,为addUI.jsp,Action为AddUI

2、在列表界面中点击修改。会弹出修改界面,为editUI.jsp,Action为EditUI

3、列表界面为list.jsp,Action为Tolist

4、在AddUI界面中添加提交,跳转到列表界面,Action为Add

5、在EditUI界面中提交,跳转到列表列表界面,Action为Edit

6、在列表界面点击删除,不跳转界面,Action为Delete


分析完成之后,我们做代码:


第一步:创建RoleAction


在cn.itcast.oa.view.action文件夹下建立一个java类,为RoleAction,将我们先前分析好的代码放在里面:


<span style="font-size:18px;">package cn.itcast.oa.view.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.oa.domain.Role;
import cn.itcast.oa.service.RoleService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Scope("prototype")
public class RoleAction extends ActionSupport implements ModelDriven<Role>{
	
	@Resource
	private RoleService roleService;
	
	private Role model = new Role();
	@Override
	public Role getModel() {
		return model;
	}

	/**列表*/
	public String list() throws Exception{
		List<Role> roleList = roleService.findAll();
		ActionContext.getContext().put("roleList", roleList);
		return "list";
	}
	
	/**删除*/
	public String delete() throws Exception{
		roleService.delete(model.getId());
		return "toList";
	}
	
	/**添加页面*/
	public String addUI() throws Exception{
		return "addUI";
	}
	
	/**添加*/
	public String add() throws Exception{
		//
//		Role role = new Role();
//		role.setName(model.getName());
//		role.setDescription(model.getDescription());
		//
		
		roleService.save(model);
		return "toList";
	}
	
	/**修改页面*/
	public String editUI() throws Exception{
		//准备回显得数据
		Role role = roleService.getById(model.getId());
		ActionContext.getContext().getValueStack().push(role);
//		this.name="abc";
//		this.description="xxx";
		return "editUI";
	}
	
	/**修改*/
	public String edit() throws Exception{
		//1,从数据库中获取元对象
		Role role =roleService.getById(model.getId());
		//2,设置要修改的属性
		role.setName(model.getName());
		role.setDescription(model.getDescription());
		
		//3,更新到数据库
		roleService.update(role);
		return "toList";
	}


}
</span>

第二步:配置文件Struts.xml:


<span style="font-size:18px;"><?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.devMode" value="true" />
    <!-- 把扩展名配置为action -->
    <constant name="struts.action.extension" value="action" />
    <!-- 把主题配置为simple -->
    <constant name="struts.ui.theme" value="simple" />
	
   
    <package name="default" namespace="/" extends="struts-default">
      
		<!-- 配置测试用的Action,未与Spring整合,class属性写类的全名 -->
		<!-- 当Struts2与Spring整合后,class属性可以写bean的名称 -->
		<action name="test" class="testAction">
			<result name="success">/test.jsp</result>
		</action>
		
		
		<!-- 岗位管理 -->
		<action name="role_*" class="roleAction" method="{1}">
			<result name="list">/WEB-INF/jsp/roleAction/list.jsp</result>
			<result name="addUI">/WEB-INF/jsp/roleAction/addUI.jsp</result>
                      <result name="addUI">/WEB-INF/jsp/roleAction/editUI.jsp</result>
                    <result name="toList" type="redirectAction">role_list</result></action>
              </package></struts></span>


第三步:jsp

list.jsp:

<span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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>  
    <title>My JSP 'index.jsp' starting page</title>
    </head>
    <body>
        
        <s:iterator value="#roleList">
            <s:property value="id"/>
            <s:property value="name"/>
            <s:property value="description"/>
            <s:a action="role_delete?id=%{id}" οnclick="return confirm('确定要删除吗?')">删除</s:a>
            <s:a action="role_editUI?id=%{id}">修改</s:a>
            <br/>
        </s:iterator>
        
        <br/>
        
        <s:a action="role_addUI">添加</s:a>
        
    </body>
</html>
</span>


addUI界面:


<span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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>  
    <title>My JSP 'index.jsp' starting page</title>
    </head>
    <body>
    	
    	<s:form action="role_add">
    		<s:textfield name="name"/><br/>
    		<s:textarea name="description"/><br/>
    		<s:submit value="提交"></s:submit>
    	</s:form>  	
    	
    </body>
</html>
</span>

editUI.jsp:


<span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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>  
    <title>My JSP 'index.jsp' starting page</title>
    </head>
    <body>
    	    <s:form action="role_edit">
    	    <s:hidden name="id"/><br/>
    		<s:textfield name="name"/><br/>
    		<s:textarea name="description"></s:textarea><br/>
    		<s:submit value="提交"></s:submit>
    	</s:form> 
    </body>
</html>
</span>

这样一个简单的SSH框架就搭建起来了。









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值