搭建ssh框架项目
Posted cppdy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搭建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; } }
(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); } }
(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; } }
(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"; } }
二、测试新增功能
三、查看数据库
四、项目结构
以上是关于搭建ssh框架项目的主要内容,如果未能解决你的问题,请参考以下文章