「java优雅业务处理类」java业务解决方案
今天给各位分享java优雅业务处理类的知识,其中也会对java业务解决方案进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
Java核心技术:Spring是什么?
从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。 简单来说,Spring就是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。 下面从整体上认识一下Spring的主要特征: *轻量:从大小与开销两方面而言Spring都是轻量的。此外,Spring是非侵入式的:使用Spring,我们的类还是pojo类,完全不用继承和实现Spring的类和接口等。 也就是说,使用Spring的应用中的对象不依赖于Spring的特定类。 *IoC:Spring通过控制反转技术促进了松耦合。当应用了IoC,一个对象依赖的其它对象会通过被动的方式传递进来,而不是这个对象自己创建或者查找依赖对象。可以认为IoC与JNDI相反--不是我们自己控制对象从容器中查找依赖,而是容器在对象初始化时不等对象请求就主动将依赖传递给它,这就是DI--依赖注入。 基本上就是对象不用自己动手管理和创建。完全由容器管理,我们只管用就行。 *AOP:Spring提供了面向切面的编程支持,AOP将与程序业务无关的内容分离提取,应用对象只实现它们应该做的--完成业务逻辑--仅此而已。它们并不负责其它的系统级关注点,例如日志或事务支持。 AOP将与业务无关的逻辑横切进真正的逻辑中。 *框架:Spring可以将简单的组件配置、组合成为复杂的应用。在Spring中,应用对象被声明式地组合,典型地是在一个XML文件里。Spring也提供了很多基础功能(事务管理、持久化框架集成等等),而用户就有更多的时间和精力去开发应用逻辑。 所有Spring的这些特征都能帮助我们够编写更干净、更可管理、并且更易于测试的代码。它们也为Spring中的各种模块提供了基础支持。 *借助Spring,荣国依赖注入,AOP应用,面向接口编程,来降低业务组件之间的耦合度,增强系统的扩展性。 * 让已有的技术和框架更加易用。 *利用其对hibernate的SessionFactory、事务管理的封装,更简洁的应用hibernate. *Spring并不完全依赖于Spring,开发者可自由选用Spring框架的部分或全部 *利用AOP思想,集中处理业务逻辑,减少重复代码,构建优雅的解决方案。 *低侵入式设计,代码污染极低。 Spring致力于J2EE应用的各层的解决方案,而不是仅仅专注于某一层的方案。可以说Spring是企业应用开发的"一站式"选择,并贯穿表现层、业务层及持久层。 虽然Spring可以一站式解决整个项目问题,但是Spring并不想取代那些已有的框架,而是与它们无缝地整合。Spring可以降低各种框架的使用难度,他提供了对各种优秀框架(如Struts、Hibernate、Hessian、Quartz等)的直接支持。 使用Spring的主要目的是使J2EE易用和促进好的编程习惯,Spring的目标就是让已有的技术更加易用。 所以Spring的一个重要思想就是整合和兼容。
Java中的业务类和实体类怎么区分
实体类:就是一个现实中的实体
它所具有的一些属性
和一些动作(方法),实体类可以和多个业务相关联,业务类中操作实体类。
业务类:就是业务所需要实现的那些功能所需要的属性和方法。
其实在java开发中
基本不会像你那么用,把属性定义在了业务类当中。
业务类就直接在方法中new一个实体类就可以了,这样做会使得业务更清晰的。
我想在Java里面实现提交的功能,不用Struts如何实现呢?
你的传值方法用反了,你上面的form表单里面写的方法是post但是你在后台Servlet处理的时候用的是doGet方法处理,是不可能接受到数据的。做这个上传东西的话,需要导入一些外包,像cosFir这种包来实现比较好一点。
如果只是实现提交功能的话,首先思路要明确,你表单的内容提交过来,要直接获取的话用你的方法就可以了,但是注意传值的方法,一般用post传值,因为get方法传值太小,容易丢失数据。还有就是你传过来了在servlet处理的时候最后弄个标记来处理,这样可以利用if分支来处理多个页面出来的的请求,当然servlet也只是初步的处理,你最后还得调用javabean来处理,就是你要用一个业务类来专门处理你的sql语句接串,然后提交数据可处理。看一下我写的一个登录的小程序:
登录页面:index.jsp
%@ page language="java" import="java.util.*" pageEncoding="GBK"%
%
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
base href="%=basePath%"
titleMy JSP 'index.jsp' starting page/title
meta http-equiv="pragma" content="no-cache"
meta http-equiv="cache-control" content="no-cache"
meta http-equiv="expires" content="0"
meta http-equiv="keywords" content="keyword1,keyword2,keyword3"
meta http-equiv="description" content="This is my page"
!--
link rel="stylesheet" type="text/css" href="styles.css"
--
/head
body
%
request.setCharacterEncoding("gbk");
String rs_flag=(String)request.getAttribute("rs_flag");
if(rs_flag!=null){
out.println(rs_flag);
}
%
请登陆:
form action="LoginServlet" name="form1" method="post"
用户名:input type="text" name="consumername"
密码:input type="password" name="consumerpsw"
input type="submit" name="sub" value="登陆"
/form
/body
/html
servlet处理Loginservlet
package com.icss.contr;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.icss.dao.UserDao;
public class LoginServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public LoginServlet() {
super();
}
/**
* Destruction of the servlet. br
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. br
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. br
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* servlet要完成用户提交信息的整理
*
*/
request.setCharacterEncoding("GBK");
String consumername=request.getParameter("consumername");
String consumerpsw=request.getParameter("consumerpsw");
int flag=0;
/*
* servlet要控制请求由那个业务类处理
*/
UserDao userdao=new UserDao();
try {
flag=userdao.validate(consumername, consumerpsw);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* servlet 要根据业务类处理的结果(判断分支)选择响应的jsp页面
*/
if(flag==0){
/*
* 在响应之前在做显示信息的准备
*/
String rs_flag="登陆失败!请重试!";
request.setAttribute("rs_flag",rs_flag);
//转发
request.getRequestDispatcher("index.jsp").forward(request,response);
}else{
request.getRequestDispatcher("main.jsp").forward(request,response);
}
}
/**
* Initialization of the servlet. br
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
然后调用javabean业务处理类Userdao来处理
package com.icss.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.icss.base.DbCon;
import com.icss.vo.UserVo;
public class UserDao extends DbCon {
public int validate(String consumername,String consumerpsw) throws ClassNotFoundException, SQLException{
getCon();
String sql="select * from consumer where consumername='"+consumername+"' and consumerpsw='"+consumerpsw+"'" ;
int flag=0;
ResultSet rs=DbCon.st.executeQuery(sql);
if(rs.next()){
flag=1;
}
rs.close();
closeCon();
return flag;
}
}
处理完数据库的接串以及查询以后返回结果到servlet做处理,然后返回jsp页面就可以了。
下面这个是连接数据库的,我不知道你用什么数据库,我用的是oracle,不过都差不多,你看一下把:
package com.icss.base;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class DbCon {
public static Connection conn=null;
public static Statement st=null;
public void getCon() throws ClassNotFoundException, SQLException{
Class.forName("oracle.jdbc.driver.OracleDriver");//创建数据库驱动对象
conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.207:1521:ICSS","TEST","MANAGER");//创建数据库连接对象
st=conn.createStatement();//创建执行sql语句对象
}
public void closeCon() throws SQLException{
if(st!=null){
st.close();
}
if(conn!=null){
conn.close();
}
}
/**
* @param args
*/
public static void main(String[] args) {
DbCon dbc=new DbCon();
try {
dbc.getCon();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
呼呼,写这个真是有点浪费时间~·不过好歹完工了,当作是复习一下。希望对你有用。。
java action类的作用
public interface Actionextends ActionListener
Action 接口提供 ActionListener 接口的一个有用扩展,以便若干控件访问相同的功能。
除了 ActionListener 接口定义的 actionPerformed
方法之外,此接口还允许应用程序在一个位置定义:
描述函数的一个或多个文本字符串。这些字符串可用于显示按钮的立体文本、在菜单项中设置文本等等。
描述函数的一个或多个图标。这些图标可用于菜单控件中的图像,或者用于更复杂用户界面中的合成项。
功能的启用/禁用状态。应用程序可以禁用实现此接口的函数,而不必分别禁用菜单项和工具栏按钮。所有为侦听状态更改而注册为侦听器的组件都将禁止为该项生成事件,并相应地修改显示。
可以将此接口添加到现有类中,或者用它创建一个适配器(通常通过子类化 AbstractAction 来实现)。然后可以将
Action 对象添加到多个可感知 Action 的容器中,并连接到可容纳
Action 的组件。然后可以通过调用 Action 对象的 setEnabled
方法立刻激活或取消激活 GUI 控件。
注意,Action 实现在存储方面的开销比典型的 ActionListener
要高,但后者不具有集中控制功能和广播属性更改的优点。因此,应该注意只在需要这些优点的地方使用 Action,在别处使用
ActionListener 即可。
支持 Action 的 Swing 组件
许多 Swing 的组件都具有 Action 属性。在组件上设置 Action
时,会发生以下几种情况:
Action 被作为 ActionListener 添加到组件。
组件配置自身的某些属性以匹配 Action。
组件在 Action 上安装 PropertyChangeListener,这样组件可更改其属性以反映 Action 属性中的更改。
java优雅业务处理类的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java业务解决方案、java优雅业务处理类的信息别忘了在本站进行查找喔。