搭建ssh框架项目(五)

一、控制层优化

  (1)创建BaseAction.java类

package com.cppdy.ssh.web.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; public void setServletRequest(HttpServletRequest request) {
this.request = request;
} public void setServletResponse(HttpServletResponse response) {
this.response = response;
} }

BaseAction.java

  (2)创建ServiceProviderCord.java类

package com.cppdy.ssh.container;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class ServiceProviderCord { protected static ApplicationContext ac; /**
* @Name: load
* @Description: 加载beans.xml文件(filename放置的是beans.xml)
* @Parameters: String filename(传递beans.xml文件)
* @Return: 无
*/
public static void load(String filename){
ac = new ClassPathXmlApplicationContext(filename);
}
}

ServiceProviderCord.java

  (3)创建ServiceProvider.java类

package com.cppdy.ssh.container;

import org.apache.commons.lang.StringUtils;

public class ServiceProvider {
public static ServiceProviderCord spc;
//加载beans.xml文件
static{
spc = new ServiceProviderCord();
spc.load("beans.xml");
}
/**
* @Name: getService
* @Description: 自定义spring容器,加载服务节点
* @Parameters: String serviceName 服务节点名称
* @Return: Object 服务接口
*/
public static Object getService(String serviceName){
if(StringUtils.isBlank(serviceName)){
throw new RuntimeException("当前服务名称不存在");
}
Object object = null;
if(spc.ac.containsBean(serviceName)){
object = spc.ac.getBean(serviceName);
}
if(object==null){
throw new RuntimeException("当前服务名称【"+serviceName+"】下的服务节点不存在");
}
return object;
}
}

ServiceProvider.java

  (4)修改ElecTextAction.java的save方法,将数据保存入库

package com.cppdy.ssh.web.action;

import java.util.Date;

import com.cppdy.ssh.container.ServiceProvider;
import com.cppdy.ssh.domain.ElecText;
import com.cppdy.ssh.service.IElecTextService;
import com.cppdy.ssh.web.form.ElecTextForm;
import com.opensymphony.xwork2.ModelDriven; @SuppressWarnings("serial")
public class ElecTextAction extends BaseAction implements ModelDriven<ElecTextForm>{ private IElecTextService elecTextService = (IElecTextService)ServiceProvider.getService("elecTextService"); private ElecTextForm elecTextForm = new ElecTextForm(); public ElecTextForm getModel() {
return elecTextForm;
}
/**
* @Name: save
* @Description: 保存ElecText对象的方法
* @Parameters: 无
* @Return: 跳转到system/textAdd.jsp
*/
public String save(){
//System.out.println(elecTextForm.getTextName());
//VO对象转换成PO对象
//实例化PO对象
ElecText elecText = new ElecText();
elecText.setTextName(elecTextForm.getTextName());//测试名称
elecText.setTextDate(new Date());//测试日期
elecText.setTextRemark(elecTextForm.getTextRemark());//测试备注 elecTextService.saveElecText(elecText);
return "save";
} }

ElecTextAction.java

二、测试新增功能

搭建ssh框架项目(五)

三、查看数据库

搭建ssh框架项目(五)

四、项目结构

搭建ssh框架项目(五)

上一篇:ThreadLocal源码分析:(二)get()方法


下一篇:ADO.NET连接池机制