SSH框架-控制层搭建

SSH框架-控制层搭建

 

1、控制层需要建一个与po类似的VO对象

package com.caicai.elec.contorl.vo;
/*
* VO 是值对象,对应页面属性值
*
* VO 对象与PO相同点与不同点
*
*    相同点:都是JAVAbean 对象
*    不同点: PO对象中的属性关系是关联数据库字段,数据库字段发生变化,PO里面的配置也要发生变化。
*            VO 对象中的属性关系是对应页面的,可以随便变化,可以任意增删修改,对应表单属性
*
*
* PO对象是处理后台逻辑的
*
* VO是处理前台显示的
*
* */

public class ElecTextVo implements java.io.Serializable {
    //定义数据库字段
    private String textID;
    private String textName;
    private String textDate;
    private String textRemark;
//配置get set 方法


    public String getTextID() {
        return textID;
    }

    public void setTextID(String textID) {
        this.textID = textID;
    }

    public String getTextName() {
        return textName;
    }

    public void setTextName(String textName) {
        this.textName = textName;
    }

    public String getTextDate() {
        return textDate;
    }

    public void setTextDate(String textDate) {
        this.textDate = textDate;
    }

    public String getTextRemark() {
        return textRemark;
    }

    public void setTextRemark(String textRemark) {
        this.textRemark = textRemark;
    }
}

2、建立一个action 

如果需要使用request获取页面参数测试的话,可以编写一个公共类

 

BaseAction

 

package com.caicai.elec.contorl.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class BaseAction extends ActionSupport implements ServletRequestAware,ServletResponseAware {
    
    
    protected HttpServletRequest request = null;
     
    
     protected HttpServletResponse response = null;

    @Override
    public void setServletRequest(HttpServletRequest request) {
        // TODO Auto-generated method stub
        this.request=request;
        
    }

    @Override
    public void setServletResponse(HttpServletResponse response) {
        // TODO Auto-generated method stub
        this.response=response;
        
    }
    
    
    
}

 

 

编写一个字符串转日期类型的方法类

StringConverDate

 

package com.caicai.elec.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;



public class StringHelper {
    
    /**
     * 将字符串转换成日期类型
     *
     */
    
    public static Date StringConverDate( String Date) {
        
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date=simpleDateFormat.parse(Date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return date;
        
    }
    
    

}

 

 

 

继承BaseAction

 

package com.caicai.elec.contorl.action;



import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.caicai.elec.contorl.vo.ElecTextVo;
import com.caicai.elec.doamin.ElecText;
import com.caicai.elec.service.IElecTextService;
import com.caicai.elec.util.StringHelper;
import com.opensymphony.xwork2.ModelDriven;



//一个action 需要继承一个Action Support

//实现一个模型驱动类
public class ElecTextAction  extends BaseAction implements ModelDriven<ElecTextVo> {

    /**
     * 
     */
    private static final long serialVersionUID = 1707074060653507324L;
    private ElecTextVo elecTextVo = new ElecTextVo();
    @Override
    public ElecTextVo getModel() {
        System.out.println("request comes in getModel");
        return elecTextVo;
    }


   
    public String save (){
//        System.out.println("request comes in save");
//        System.out.println(elecTextVo.getTextName());
        //获取标签内容,使用request方式
//        System.out.print(request.getParameter("textRemark"));
        //Vo对象转换成PO对象 , 需要将输入的值给到持久层
         //实例化PO对象
         ElecText elecText = new ElecText();
         elecText.setTextName(elecTextVo.getTextName());//测试名称
         elecText.setTextDate(StringHelper.StringConverDate(elecTextVo.getTextDate()));//测试日期
         elecText.setTextRemark(elecTextVo.getTextRemark());//备注
         
         //引入Spring注解,调用service
         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
         IElecTextService elecTextService =  (IElecTextService)applicationContext.getBean(IElecTextService.IELecTextservice);
         elecTextService.saveElecText(elecText);
        return "save";

    }
}

3、创建struts.xml并且配置

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
    <!-- 配置action的访问路径为.do的形式 -->
    <constant name="struts.action.extension" value="do"></constant>
    <!-- 配置struts的开发模式 -->
    <constant name="struts.devMode" value="true"></constant>
    <!-- 配置struts的简单模式 -->
    <constant name="struts.ui.theme" value="simple"></constant>
    <package name="system" namespace="/system" extends="struts-default">
        <action name="elecTextAction_*" class="com.caicai.elec.contorl.action.ElecTextAction" method="{1}">
            <result name="save">
                /system/textAdd.jsp
            </result>
        </action>
    </package>
</struts>

 

4、配置struts过滤器在web.xml中

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>dainli20210421</display-name>
  <welcome-file-list>
    <welcome-file>/system/textAdd.jsp</welcome-file>
  </welcome-file-list>
  <!-- 配置struts2 过滤器 -->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
       <filter-name>struts2</filter-name>
       <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

 

5、配置jps页面

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="java" pageEncoding="UTF-8"%>
   <script type="text/javascript" language="JavaScript" src="${pageContext.request.contextPath }/script/calendar.js" charset="gb2312"></script>
<html>
<head>
<title>测试专用jsp</title>
<link href="${pageContext.request.contextPath }/css/Style.css" type="text/css" rel="stylesheet">

  <script language="javascript"> 
  function checkchar(){
     document.Form1.action="system/elecTextAction_save.do";
     document.Form1.submit();
  //alert(" 保存成功!");
  }
  function addEnter(element){
     document.getElementById(element).value = document.getElementById(element).value+"<br>";
   
  }
  </script>


</head>

<body>
<form name="Form1" id="Form1" method=post>

    <table cellspacing="1" cellpadding="5" width="90%" align="center" bgcolor="#f5fafe" style="border:1px solid #8ba7e3" border="0">

        <tr>
            <td class="ta_01" colspan=2 align="center" background="${pageContext.request.contextPath }/images/b-info.gif">
            <font face="宋体" size="2"><strong>测试专用jsp</strong></font>
            </td>
        </tr>
        <TR height=10><td></td><td></td></TR>
        
        <tr>
            <td class="ta_01" align="center" bgcolor="#f5fafe" width="15%">测试名称:</td>
            <td class="ta_01" bgcolor="#ffffff" style="word-break: break-all">
    
            <textarea name="textName" id="textName"   style="width: 500px; height: 160px; padding: 1;FONT-FAMILY: 宋体; FONT-SIZE: 9pt" onkeydown="if(event.keyCode==13)addEnter('textName');"></textarea>
            </td>
            
        </tr>
        <tr>
            <td class="ta_01" align="center" bgcolor="#f5fafe" width="15%">测试日期:</td>
            <td class="ta_01" bgcolor="#ffffff" style="word-break: break-all">
    
            <input name="textDate" type="text" maxlength="50" size=20 onclick="JavaScript:calendar(document.Form1.textDate)">
            </td>
            
        </tr>
        <tr>
            <td class="ta_01" align="center" bgcolor="#f5fafe" width="15%">测试备注:</td>
            <td class="ta_01" bgcolor="#ffffff" style="word-break: break-all">
            <textarea name="textRemark" id="textRemark"  style="width: 500px; height: 160px; padding: 1;FONT-FAMILY: 宋体; FONT-SIZE: 9pt" onkeydown="if(event.keyCode==13)addEnter('textRemark');"></textarea>
            </td>
            
        </tr>
        <tr>
            <td class="ta_01" style="width: 100%" align="center" bgcolor="#f5fafe" colspan="2">
            <input type="button" name="BT_Submit" value="保存" onclick="checkchar()" id="BT_Submit" style="font-size:12px; color:black; height=20;width=50">
            </td>
        </tr>
    </table>
      
</form>

</body>
</html>

 

在项目开发中,能做封装的就尽量封装

封装这两个方法

//加载配置文件  
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
//获取javabean IElecTextDao iElecTextDao
= (IElecTextDao)applicationContext.getBean(IElecTextDao.serivicename);

 

package com.caicai.elec.container;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ServiceProviderCord {
    
protected  static ApplicationContext applicationContext;
    
    public static  void loadXML(String filename) {
        //加载beans.xml文件(filenema是Spring 配置文件(beans.xml))
        applicationContext = new ClassPathXmlApplicationContext(filename);    
        
    }
    
    

}

 

 

package com.caicai.elec.container;

import org.apache.commons.lang.StringUtils;

public class ServiceProvider{
    //加载配置文件
    public static ServiceProviderCord serviceProviderCord;
    //在执行方法之前,程序默认调用先调用Static方法
    static{
      System.out.print("先执行static");     
      serviceProviderCord = new ServiceProviderCord();    
      serviceProviderCord.loadXML("beans.xml");
        
    }
    
    public static Object getSetvice(String ServiceName) {
        System.out.print("第二次执行");
        //判断服务名称是否为空
        if(StringUtils.isBlank(ServiceName)){
            throw new  RuntimeException("当前服务名,不存在");
            
        }
        Object object = null;
        //判断当前是否包含节点名称
        if(serviceProviderCord.applicationContext.containsBean(ServiceName)){
            
            object = serviceProviderCord.applicationContext.getBean(ServiceName);
            
        }
        
        if(object == null){
            
            throw new RuntimeException("当前服务名称 ["+ServiceName+" ]下的服务名不存在");
        }
        
        return object;
        
    }
    

}

 

posted @ 2021-04-25 20:40  菜菜920  阅读(91)  评论(0编辑  收藏  举报