本文目錄一覽:
- 1、如何在JavaWeb程序中使用tld文件
- 2、Javaweb項目中頁面展示是用什麼文件
- 3、java web 大文件上傳下載
- 4、Java web開發項目中各文件的作用
- 5、javaweb項目導出exce文件
- 6、基於ssh框架的javaweb項目配置文件都有哪些?
如何在JavaWeb程序中使用tld文件
tld定義格式
?xml version=”1.0″ encoding=”ISO-8859-1″ ?
!DOCTYPE taglib
PUBLIC “-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN”
“”
taglib
!–定義標籤版本庫–
tlib-version1.0/tlib-version
!–定義jsp版本庫–
jsp-version1.2/jsp-version
short-nameCms Tag/short-name
description!–標籤描述—
A simple appbase tag library
/description
tag
namepage/name!–tag的名字–
tag-classcom.cms.common.tag.PageTag/tag-class!–tag對應的java類的名字–
body-contentempty/body-content
!–關於body-content 有三個值可選;empty:標籤體必須為空;jsp:標籤由其他jsp元素組成比如標籤中含有%=attributeName%的jsp元素,那麼此時body-content的值就是實際attributeName傳入的值;tagdependent:有標籤解釋不帶jsp轉換(這個深入的含義不太了解)如果想深入交流技術可以加群前面435中間416最後141組合到一起就可以找到了–
attribute!—這裡表示的是這個tag的一個參數–
namecmsform/name!–這個參數的名字–
requiredtrue/required!–是否是必填選項–
rtexprvaluetrue/rtexprvalue!–這個參數的值是否可以寫入,換句話說 就是這個參數是否可以動態賦值–
/attribute
/tag
/taglib
2、定義Tag對應類
此類必須重寫doStartTag以及doEndTag方法
/**
*
*/
package com.cms.common.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
* @author louisliao
*
*/
public class DemoViewTag extends TagSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String cmsform = “”;
public String getCmsForm() {
return cmsform ;
}
public void setCmsForm(String cmsform ) {
this.cmsform = cmsform ;
}
/**
*
*/
public DemoViewTag() {
// TODO Auto-generated constructor stub
}
public int doStartTag()
{
return super.SKIP_BODY;
}
public int doEndTag() throws JspException
{
JspWriter writer=pageContext.getOut();
try {
writer.print(“這是我的標籤示例br/”+”cmsform :”+this.cmsform);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return super.EVAL_PAGE;
}
}
3、在web.xml中加入taglib對應文件配置 如:
taglib
taglib-uri;/taglib-uri
taglib-location/WEB-INF/mytag.tld/taglib-location
/taglib
示例:
定義myTag.tld標籤文件
?xml version=”1.0″ encoding=”utf-8″?
!DOCTYPE taglib PUBLIC “-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN”
“”
taglib
tlibversion1.0/tlibversion
jspversion1.1/jspversion
shortnameMyJSPTag Library/shortname
uri;/uri
info我的示例標籤庫/info
tag
namedemo.Viewport/name
tagclasscom.myapp.web.tag.DemoViewTag/tagclass
bodycontentJSP/bodycontent
infodemo.Viewport標籤/info
attribute
namenorthTitle/name
requiredtrue/required
rtexprvaluetrue/rtexprvalue
/attribute
attribute
namewestTitle/name
requiredtrue/required
rtexprvaluetrue/rtexprvalue
/attribute
/tag
/taglib
定義標籤類
/**
*
*/
package com.myapp.web.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
* @author louisliao
*
*/
public class DemoViewTag extends TagSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String northTitle = “”;
private String westTitle = “”;
public String getNorthTitle() {
return northTitle;
}
public void setNorthTitle(String northTitle) {
this.northTitle = northTitle;
}
public String getWestTitle() {
return westTitle;
}
public void setWestTitle(String westTitle) {
this.westTitle = westTitle;
}
/**
*
*/
public DemoViewTag() {
// TODO Auto-generated constructor stub
}
public int doStartTag()
{
return super.SKIP_BODY;
}
public int doEndTag() throws JspException
{
JspWriter writer=pageContext.getOut();
try {
writer.print(“這是我的標籤示例br/westTitle:”+this.westTitle+”br/northTitle:”+this.northTitle);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return super.EVAL_PAGE;
}
}
web.xml添加配置
taglib
taglib-uri;/taglib-uri
taglib-location/WEB-INF/mytag.tld/taglib-location
/taglib
測試頁面
%@ page language=”java” import=”java.util.*” pageEncoding=”utf-8″%
%@ taglib uri=”” prefix=”myTag”%
%
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 ‘tagtldDemo.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
This is my JSP page. br
myTag:demo.Viewport northTitle=”南” westTitle=”西”/myTag:demo.Viewport
/body
/html
Javaweb項目中頁面展示是用什麼文件
使用jsp和html展示內容以及和後端交互。
html是靜態頁面,jsp是動態頁面,日常開發中,這兩種都是結合使用的。
JSP全名為JavaServerPages,中文名叫java服務器頁面,其根本是一個簡化的設計,它 是由Sun Microsystems公司倡導、許多公司參與一起建立的一種動態網頁技術標準。JSP技術有點類似ASP技術,它是在傳統的網頁HTML標準通用標記語言的子集文件*.htm,*.html中插入Java程序段Scriptlet和JSP標記tag,從而形成JSP文件,後綴名為(*.jsp),用JSP開發的Web應用是跨平台的,既能在Linux下運行,也能在其他操作系統上運行。
java web 大文件上傳下載
直接把大文件讀取為IO流,之後進行上傳下載即可,不用擔心文件大,是可以分流下載上傳的(setBufferSize(1024))。
舉例:
import hkrt.b2b.view.util.Log;
import hkrt.b2b.view.util.ViewUtil;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.commons.net.;
import org.apache.commons.net.;
public class CCFCCBFTP {
/**
* 上傳文件
*
* @param fileName
* @param plainFilePath 明文文件路徑路徑
* @param filepath
* @return
* @throws Exception
*/
public static String fileUploadByFtp(String plainFilePath, String fileName, String filepath) throws Exception {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FTPClient ftpClient = new FTPClient();
String bl = “false”;
try {
fis = new FileInputStream(plainFilePath);
bos = new ByteArrayOutputStream(fis.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = fis.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
Log.info(“加密上傳文件開始”);
Log.info(“連接遠程上傳服務器”+CCFCCBUtil.CCFCCBHOSTNAME+”:”+22);
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD);
FTPFile[] fs;
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(filepath)) {
bl=”true”;
ftpClient.changeWorkingDirectory(“/”+filepath+””);
}
}
Log.info(“檢查文件路徑是否存在:/”+filepath);
if(“false”.equals(bl)){
ViewUtil.dataSEErrorPerformedCommon( “查詢文件路徑不存在:”+”/”+filepath);
return bl;
}
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding(“GBK”);
// 設置文件類型(二進制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile(fileName, fis);
Log.info(“上傳文件成功:”+fileName+”。文件保存路徑:”+”/”+filepath+”/”);
return bl;
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
}
}
/**
*下載文件
*
* @param localFilePath
* @param fileName
* @param routeFilepath
* @return
* @throws Exception
*/
public static String fileDownloadByFtp(String localFilePath, String fileName,String routeFilepath) throws Exception {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FileOutputStream fos = null;
FTPClient ftpClient = new FTPClient();
String SFP = System.getProperty(“file.separator”);
String bl = “false”;
try {
Log.info(“下載並解密文件開始”);
Log.info(“連接遠程下載服務器”+CCFCCBUtil.CCFCCBHOSTNAME+”:”+22);
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD);
// ftpClient.connect(CMBCUtil.CMBCHOSTNAME, 2021);
// ftpClient.login(CMBCUtil.CMBCLOGINNAME, CMBCUtil.CMBCLOGINPASSWORD);
FTPFile[] fs;
ftpClient.makeDirectory(routeFilepath);
ftpClient.changeWorkingDirectory(routeFilepath);
bl = “false”;
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
bl = “true”;
Log.info(“下載文件開始。”);
ftpClient.setBufferSize(1024);
// 設置文件類型(二進制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
InputStream is = ftpClient.retrieveFileStream(fileName);
bos = new ByteArrayOutputStream(is.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = is.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
fos = new FileOutputStream(localFilePath+SFP+fileName);
fos.write(bos.toByteArray());
Log.info(“下載文件結束:”+localFilePath);
}
}
Log.info(“檢查文件是否存:”+fileName+” “+bl);
if(“false”.equals(bl)){
ViewUtil.dataSEErrorPerformedCommon(“查詢無結果,請稍後再查詢。”);
return bl;
}
return bl;
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (fos != null) {
try {
fos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
}
}}
備註:以上方法就實現了流的二進制上傳下載轉換,只需要將服務器連接部分調整為本地的實際ftp服務用戶名和密碼即可。
Java web開發項目中各文件的作用
JSP 所有的網頁代碼
js所有的javaScript代碼
bo一般是java的類文件
dao一般是處理數據庫方法的接口
impl是上述接口的實現
service 是 處理數據庫邏輯的服務
action 是處理業務邏輯的
xml是配置文件
jsp得到請求到 structs的配置文件里找相應的配置方法,根據配置方法到action里找到處理的方法,然後執行,返回一個結果,再到剛才的配置文件里查找返回處理結果,然後返回jsp頁面上
這其中,action里的處理的時候,可能會跟數據庫發生交互,就會調用service里的方法,service里的方法又會通過接口調用impl里的方法,然後bo里是與數據庫一一對應的映射文件,對應着數據庫里的每張表
javaweb項目導出exce文件
java導出Excel需要用到poi的jar包,
// 第一步,創建一個webbook,對應一個Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一個sheet,對應Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(“學生表一”);
// 第三步,在sheet中添加表頭第0行,注意老版本poi對Excel的行數列數有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,創建單元格,並設置值表頭 設置表頭居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 創建一個居中格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue(“學號”);
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue(“姓名”);
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue(“年齡”);
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue(“生日”);
cell.setCellStyle(style);
// 第五步,寫入實體數據 實際應用中這些數據從數據庫得到,
List list = CreateSimpleExcelToDisk.getStudent();
for (int i = 0; i list.size(); i++)
{
row = sheet.createRow((int) i + 1);
Student stu = (Student) list.get(i);
// 第四步,創建單元格,並設置值
row.createCell((short) 0).setCellValue((double) stu.getId());
row.createCell((short) 1).setCellValue(stu.getName());
row.createCell((short) 2).setCellValue((double) stu.getAge());
cell = row.createCell((short) 3);
cell.setCellValue(new SimpleDateFormat(“yyyy-mm-dd”).format(stu
.getBirth()));
}
// 第六步,將文件存到指定位置
try
{
FileOutputStream fout = new FileOutputStream(“E:/students.xls”);
wb.write(fout);
fout.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
基於ssh框架的javaweb項目配置文件都有哪些?
三大框架需要用到的配置文件總共有一下幾個:struts.xml、hibernate-cfg.xml 、spring.xml 、web.xml。添加了配置文件其實只是其中的一個步驟,第二步,應該添加各個框架的jar包(添加需要用到的就行)。 這樣,三個框架的環境才算配置完成。
原創文章,作者:ECCT,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/139218.html