sql += " and HT_CGFKSQ.sqsj >=to_date(‘"+kssj+"‘,‘yyyy-MM-dd‘)";
java.sql.Date.valueOf(qjdpo.getKssjStr())
String nowRq = new SimpleDateFormat("yyyy-MM-dd")
.format(new java.util.Date());// 当前的日期
public void addFksqxx(CghtfksqPO cghtfksqPO)throws DataAccessException, SQLException {
if (cghtfksqPO == null) {
throw new IllegalArgumentException(cghtfksqPO + "对象不能为空!");
}
pstmt.setDate(1, java.sql.Date.valueOf(cghtfksqPO.getFksjStr()));
pstmt.setDouble(2, Double.parseDouble(cghtfksqPO.getFkjeStr()));
pstmt.setString(3, cghtfksqPO.getSqr());
pstmt.setDate(4,java.sql.Date.valueOf(nowRq));
pstmt.setString(5, htbh);
// pstmt.setDate(6, java.sql.Date.valueOf(fksj));
pstmt.execute();
TransactionManager.commitTransaction();
注意不能直接使用form表单的reset方法进行处理,而应该以如下方式进行调用,
Document.form[…].validatorGroup.reset();
<%@ page isELIgnored="false"%>在页面中启用EL表达式
利用Iterator实现对List的循环遍历:
List<String> all=new ArrayList<String>();
all.add("Hello");
all.add(" World !");
Iterator<String> it=all.iterator();
while (it.hasNext()) {
System.out.print(it.next());
}
Int和string类型转换
int lsh
ps.setString(1, String.valueOf(lsh));
String optionId=Integer.valueOf(questOption.getId()).toString();
int pictureId=Integer.parseInt(request.getParameter("pictureId"));
pages = String.valueOf(pagesDefault);
(new Integer(pages)).intValue()
rows = StringUtil.trim(rows);
替换
nr.replaceAll("___", zzrq)
解决乱码问题
xmz = java.net.URLDecoder.decode(request.getParameter("xmz"),"UTF-8");
response.setContentType("text/text;charset=UTF-8");
页面业务逻辑处理,绕过Action和form
<body>
<%
/****根据主页面传过来员工号获取该员工对应的信息****/
String ygdh = request.getParameter("ygdh");
YgzhxxcxDAO yd = YgfwDAOFactory.getYgfwDAOFactory()
.getYgzhxxcxDAO();
try {
//成绩查询结果List
List<KsScorePO> ksScoreList = yd.cjxx(ygdh);
request.setAttribute("ksList", ksScoreList);
} catch (Exception e) {
//若存在异常,打印输出异常
out.print(e.getMessage());
}
%>
<logic:notEmpty name="ksList">
<logic:iterate name="ksList" id="po">
<tr>
<td nowrap="nowrap" style="text-align: left">
<bean:write name="po" property="ksmc" />
</td>
StringBuffer scores = new StringBuffer();
while(rs1.next()){
scores.append(String.format("%.2f", rs1.getDouble(1))).append("M");
}
if(scores.length() > 1){
scores.deleteCharAt(scores.length() - 1);
}
po.setSjbp_dthsStr(String.valueOf(rs0.getInt(2))); //答题时间
po.setSjbp_jjsjStr(String.valueOf(rs0.getDate(4))); //交卷时间
po.setKssj(String.valueOf(rs0.getTimestamp(6)));//开考时间
po.setDbfs(String.format("%.2f", Double.parseDouble(rs0.getInt(7)+"")));//达标分数
public String getKshs(String kshs) {
int dths = Integer.parseInt(kshs);
int minutes = (dths - (3600 * hours)) / 60;
if (minutes > 0) {
if (String.valueOf(minutes).length() == 1) {
minute = "0" + minutes + "分";
} else {
minute = minutes + "分";
}
Struts将MVC的Controller一分为三。
在Struts中,将常规情况下的Servlet拆分为ActionServlet、FormBean、ActionBean三个部分。
ActionServlet配合Struts-config.xml,专职完成页面导航,而不再负责具体的数据获取与相应逻辑,这两部分功能由FormBean和ActionBean来完成。
将资源文件中的中文转变成ASCII码:命令
¡ Native2ascii –encoding gbk applicationResource_zh_CN.properties
一个应用系统的消息转移(或者说状态转移)的非持久性数据存储,是由ActionForm Bean的负责保持的。
NumberFormatException//(数据转换异常)数据转换时处理异常用
if (new java.util.Date().getTime() - rs.getDate("qjd_jssj").getTime() <= 24 * 60 * 60
* 1000 * 5 && "3".equals(rs.getString("qjd_spzt"))) {
po.setXjbz1("1");
}
ajax处理后台乱码
PrintWriter pw = null;
response.setContentType("text/text;charset=UTF-8");// /ajax处理后台乱码
sb.append(sb1.toString() + sb2.toString() + sb3.toString());
从后台向前台拼html标签
1.前台jsp获取后台传过来的标签字符串
<%
if(request.getAttribute("str") != null){
out.print(request.getAttribute("str").toString());
}
%>
2、后台拼接
sb.append(sb1.toString() + sb2.toString() + sb3.toString());
request.setAttribute("str", sb.toString());
EL表达式:
${xsstForm.nd}
循环中实现某一项的累积:
list = gzlfltjcxBO.gzlxxmcxlist(zzjgbh, ksrq, jsrq, xmbh);
for (int i = 0; i < list.size(); i++) {
gzzl = gzzl + Float.valueOf(list.get(i).getGzl());
}
for (int i = 0; i < list.size(); i++) {
list.get(i).setGzlbl(
Float.valueOf(list.get(i).getGzl()) / gzzl);
}
request.setAttribute("gzlxlist", list);
kqxxclfkListForm.setYf(month < 10? "0" + month: month + "");
if(kqxxclfkPOList.isEmpty())
DataAccessException, SQLException, ParseException
String start = year + "-" + month + "-01";
String end = DateFormatUtil.getMonthL(start);
day = rs.getDate("rq").toString().substring(8,10);
public String getZtmc(String ztdh,String all){
String[] dhAndMc = all.split("_");
int dh = dhAndMc[0].indexOf(ztdh);
return (dhAndMc[1].split(","))[dh];
}
//小数点型数据计算
/**
* 提供精确的乘法运算
*
* @param v1
* 被乘数
* @param v2
* 乘数
* @return 两个参数的积
*/
public double mul(double v1, double v2) {
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.multiply(b2).doubleValue();
}
调用处:10000
xsdzprywhPO.setJhrw(String.format("%.2f", mul(rs.getDouble("jhrw"), 0.0001)));
结果为:1
//获取登录用户
RBACRequestContext rbacRequest = WebRBACRequestContext .getRBACRequestContext(request);
User user = rbacRequest.getRequestUser();// 获取当前登陆用户
String loginName = user.getLoginName();// 登陆人的名字
/**
* 金额转人民币大写
*
* @param n
* @return String
*/
public static String digitUppercase(double n) {
String fraction[] = { "角", "分" };
String digit[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
String unit[][] = { { "元", "万", "亿" }, { "", "拾", "佰", "仟" } };
n = Math.abs(n);
String s = "";
double frac = 0;
/**拼接角分值,实现小数部分的转化*/
for (int i = 0; i < 2; i++) {
if (i == 0) {
//取角,下取整
frac = Math.floor(n * 10) % 10;
} else if (i == 1) {
//取分,下取整,*100时会产生精度问题,所以调用了mul()方法
frac = Math.floor(mul(n,100)) % 10;
}
//拼接大写和单位
s += digit[(int) frac] + fraction[i];
s = s.replaceAll("(零.)+", "");
}
//没有零头时
if (s.length() < 1) {
s = "整";
}
//获取整数部分
int integerPart = (int) Math.floor(n);
for (int i = 0; i < unit[0].length && integerPart > 0; i++) {
String p = "";
for (int j = 0; j < unit[1].length && n > 0; j++) {
p = digit[integerPart % 10] + unit[1][j] + p;
integerPart = integerPart / 10;
}
s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i]
+ s;
}
//s = s.replaceAll("(零.)*零元", "元");
while(s.matches("^零.*")){
s = s.replaceAll("^零.", "");//字符串计算去掉开头的零字,或零仟零佰等
}
s = s.replaceAll("(零.)+", "零");//字符串中出现类似:零什,零佰等的情况,将“零什”替换成“零”字
s = s.replaceAll("^整$", "零元整");//字符串中只有一个“整”字的情况,将整个字符串替换为“零元整”
s = s.replaceAll("零整$", "元整");//解决当金额超过一万时的零整问题
return s;
}
/**
* 提供精确的乘法运算。
* @param v1 被乘数
* @param v2 乘数
* @return 两个参数的积
*/
public static double mul(double v1,double v2){
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.multiply(b2).doubleValue();
}
//获取目录路径
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
List<XsrwzbjhwhPO> jp = new ArrayList<XsrwzbjhwhPO>();
ThglDAO dao = new ThglDAO();
jp = dao.getXsry();
<%=jp.get(i).getYgname().trim()%>
//根据日期获取要考勤的年月,如果是1月份,则取上一年的12月份考勤数据
if (calendar.get(Calendar.MONTH) == 0) {
kqshnd = calendar.get(Calendar.YEAR) - 1 + "";
kqshyd = "12";
} else {
kqshnd = calendar.get(Calendar.YEAR) + "";
if (calendar.get(Calendar.MONTH) < 10) {
kqshyd = "0" + calendar.get(Calendar.MONTH);
} else {
kqshyd = calendar.get(Calendar.MONTH) + "";
}
}
grjkglPO.setCjsj(rs.getDate("Jhk_cjsj") + " "
+ rs.getTime("Jhk_cjsj").toString())
String gzrq =
baseGzjlrzpPO.setGzrq(Date.valueOf(gzrq));
String tjsj = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
baseGzjlrzpPO.setTjsj(Timestamp.valueOf(tjsj));
//合并行显示
// 声明过程活动的集合,将不同的过程活动放入该集合
List<String> gchdxs = new ArrayList<String>();
gchdxs.add(xmydygcPOList.get(0).getGcmc());
// 循环将不同的过程活动放入该集合
for (int i = 0; i < xmydygcPOList.size(); i++) {
if (!gchdxs.contains(xmydygcPOList.get(i).getGcmc())) {
gchdxs.add(xmydygcPOList.get(i).getGcmc());
}
}
/* 使每个过程对应多个结果list以便于合并单元格显示 */
for (int m = 0; m < gchdxs.size(); m++) {
// 过程活动
String gchdx = gchdxs.get(m);
/* 将过程活动相同的记录放入同一个list */
for (int i = 0; i < xmydygcPOList.size(); i++) {
if (gchdx.equals(xmydygcPOList.get(i).getGcmc())) {
zjList.add(xmydygcPOList.get(i));
fl = xmydygcPOList.get(i).getFl();
}
}
XmydygcPO xmydygcPO = new XmydygcPO();
// xmydygcPO.setGchdx(gchdx);
xmydygcPO.setGcmc(gchdx);
xmydygcPO.setCount(zjList.size());
xmydygcPO.setGchdList(zjList);
xmydygcPO.setFl(fl);
// 一个过程活动对应一个剪裁申请List【包含该过程活动对应的多条剪裁申请记录】
// 将结果放入要返回的returnList
returnList.add(xmydygcPO);
zjList = new ArrayList<XmydygcPO>();
}
String xmbh = java.net.URLDecoder.decode(request.getParameter("xmbh"),"UTF-8").trim();//从前台获取输入的项目编号
for (FxjhPO po :fxjhPOList) {
catch (SQLException e) {
// 操作失败时给出用户提示
this.printSuccess(request, "",
"com.cvicse.cvicdns.common.noQueryList");
String exStr=String.valueOf(e.getMessage());
if ("0".equals(exStr)) {
message="保存失败,您修改或删除的记录已被使用!";
}else {
e.printStackTrace();
super.exceptionOperate(request, XmcszdwhAction.class, e, operFlag);
}
//查询根目录:返回类似 /cvicdns
String path = request.getContextPath();
//为List中的属性赋值
for(XmzlkpPO po:xmzlkpPOList){
if(po.getKpzb().equals(kpzb_mc)){//存在相应的考评指标名称
po.setSjdf(zlkp_sjdf);//设置实际得分
po.setSm(zlkp_sm);//设置实际得分说明
}
}
//字符串以指定的字符串开头
gs.startsWith("$sql:")
Pattern pattern = Pattern.compile("[^\\{]*[\\}$]"); //正则表达式匹配格式???
Matcher matcher = pattern.matcher(gs);
while(matcher.find()){
String srcStr = matcher.group();//找到匹配的子串
csList.add(srcStr.substring(0,srcStr.length()-1));//将匹配的结果放入List,并把子串最后的右括号去掉
}
//直接为List赋值
for (int i = 0; i < csList.size(); i++) {
if (!csList2.contains(csList.get(i))) {
csList2.add(csList.get(i));
}
}
//字符串转变成字符数组
char[] arr = str.toCharArray();
//文件流、比特流、String之间的转换
//将file变成 byte
public static byte[] getBytesFromFile(File f) {
if (f == null) {
return null;
}
try {
FileInputStream stream = new FileInputStream(f);
ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = stream.read(b)) != -1)
out.write(b, 0, n);
stream.close();
out.close();
return out.toByteArray();
} catch (IOException e) {
}
return null;
}
//将byte变成file
public File getFileFromBytes(byte[] b, String outputFile) {
BufferedOutputStream stream = null;
File file = null;
try {
file = new File(outputFile);
FileOutputStream fstream = new FileOutputStream(file);
stream = new BufferedOutputStream(fstream);
stream.write(b);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return file;
}
//将inputStream变成byte
public byte[] InputStreamTOByte(InputStream in) throws IOException{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[BUFFER_SIZE];
int count = -1;
while((count = in.read(data,0,BUFFER_SIZE)) != -1)
outStream.write(data, 0, count);
data = null;
return outStream.toByteArray();
}
//将byte变成inputStream
public InputStream byteTOInputStream(byte[] in) throws Exception{
ByteArrayInputStream is = new ByteArrayInputStream(in);
return is;
}
/**
* 普通方法加密
* @return
*/
public String addNormalSecurity(String str){
String result = "";
char[] arr = str.toCharArray();
for(int i =0; i < arr.length; i ++){
arr[i] = (char) (arr[i] ^ ‘t‘);
}
result = new String(arr);
return result;
}
/**
* 解密普通方法的加密
* @return
*/
public String unAddNormalSecurity(String str){
String result = "";
char[] arr = str.toCharArray();
for(int i =0; i < arr.length; i ++){
arr[i] = (char) (arr[i] ^ ‘t‘);
}
result = new String(arr);
return result;
}
//inputStream 变成 string
public String InputStreamTOString(InputStream in) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[BUFFER_SIZE];
int count = -1;
while((count = in.read(data,0,BUFFER_SIZE)) != -1)
outStream.write(data, 0, count);
data = null;
return new String(outStream.toByteArray(),"ISO-8859-1");
}
//string 变成 inputStream
public InputStream StringTOInputStream(String in) throws Exception{
ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes("ISO-8859-1"));
return is;
}
//byte 变成 string
public String byteTOString(byte[] in) throws Exception{
InputStream is = byteTOInputStream(in);
return InputStreamTOString(is);
}
//异步请求
$.ajax({
type:"POST",
async:false,
url:"/cvicdns/xm_xmgl/ztbgLyxgf.do?operFlag=audit",
data:{lylsh:lsh,inid:inid},
success:function(data){
yl=data;
}
});
if(yl=="1"){
alert("有与之关联的关键依赖,不允许删除!");
return;
}
//List排序
/**
* 集合排序
*
* @param xsyjcxPOList
* @param property 为XsyjcxPO中的一个属性
* @param isAsc
* @return
*/
public List<XsyjcxPO> sortList(List<XsyjcxPO> xsyjcxPOList, String property, boolean isAsc) {
Comparator beanComparator = new BeanComparator(property);
Comparator reComparator = new ComparatorChain(beanComparator, !isAsc);
Collections.sort(xsyjcxPOList, reComparator);
return xsyjcxPOList;
}
//判断字符串以XXX开头
String fname = rs.getString("fname");
if (fname.startsWith("特殊费用额度批准附件_") && "tsfy".equals(fjlb)) {}
//通过URL获取上传文件名
1、jsp 前台
$.post(url,{operFlag:‘isExist‘,inid:$("#inid").val(),fjmc:encodeURI($("#xmjhbgFile").val()),fjlb:‘bgfj‘,bgdbh:$("#bgdbh").val()},function(data){}
2、java后台
// 附件名称
String fjmc = java.net.URLDecoder.decode(request
.getParameter("fjmc"), "utf-8");
int i = fjmc.lastIndexOf("\\");
fjmc = fjmc.substring(i + 1);