
JSP输出器实例
选择输出类型并输入内容后,点击"执行输出"结果
// 示例数据
const sampleData = {
text: "这是一个简单的文本输出示例,演示JSP的out.print()方法" html: "
这是通过JSP输出的HTML内容
{ id: 1, name: "张三" age: 25, department: "技术部" { id: 2, name: "李四"e: 30, department: "部" },
{ id: 3, name: "五" age: 28, department: "产品部" ],
json: {
status: "success" data: {
users: [
{ id: 1, username: "user1": "user1@example.com" },
{ id: 2, username: "2" email: "2@example.com" ],
timestamp: new Date().toISOString()
}
}
};
// DOM元素
const outputTypeSelect = document.getElementById('outputType');
const contentInput = document.getElementById('contentInput');
const outputBtn = document.getElementById('outputBtn');
const clearBtn = document.getElementById('clearBtn');
const copyBtn = document.getElementById('copyBtn');
const exportBtn = document.getElementById('exportBtn');
const outputArea = document.getElementById('outputArea');
const themeToggle = document.getElementById('themeToggle');
const settingsBtn = document.getElementById('settingsBtn');
const autoRefresh = document.getElementById('autoRefresh');
const fontSize = document.getElementById('fontSize');
// 初始化
function init() {
loadSampleContent();
setupEventListeners();
applySettings();
}
// 加载示例内容
function loadSampleContent() {
const currentType = outputTypeSelect.value;
contentInput.value = typeof sampleData[currentType] === 'string'
sampleData[currentType]
: JSON.stringify(sampleData[currentType], null, 2);
}
// 设置事件监听器
function setupEventListeners() {
outputTypeSelect.addEventListener('change', loadSampleContent);
outputBtn.addEventListener('click', handleOutput);
clearBtn.addEventListener('click', handleClear);
copyBtn.addEventListener('click', handleCopy);
exportBtn.addEventListener('click', handleExport);
themeToggle.addEventListener('click', toggleTheme);
fontSize.addEventListener('input', updateFontSize);
}
// 处理输出
function handleOutput() {
const type = outputTypeSelect.value;
const content = contentInput.value;
let outputHTML = '';
switch(type) {
case 'text':
outputHTML = `
`;break;
case 'html':
outputHTML = `
break;
case 'list':
try {
const listData = JSON.parse(content);
outputHTML = '
listData.forEach(item => {
outputHTML += `
});
outputHTML += '
} catch (e) {
outputHTML = '
}
break;
case 'table':
try {
const tableData = JSON.parse(content);
outputHTML = '
// 表头
Object.keys(tableData[0]).forEach(key => {
outputHTML += `
});
outputHTML += '
// 表格内容
tableData.forEach(row => {
outputHTML += '
Object.values(row).forEach(value => {
outputHTML += `
});
outputHTML += '
});
outputHTML += '
} catch (e) {
outputHTML = '
}
break;
case 'json':
try {
const jsonData = JSON.parse(content);
outputHTML = `
`;} catch (e) {
outputHTML = '
}
break;
}
outputArea.innerHTML = outputHTML;
outputArea.style.fontSize = `${fontSize.value}px`;
}
// 处理清空
function handleClear() {
contentInput.value = '';
outputArea.innerHTML = `
选择输出类型并输入内容后,点击"输出"查看结果
`;
}
// 处理复制
function handleCopy() {
const textToCopy = outputArea.textContent || outputArea.innerText;
navigator.clipboard.writeText(textToCopy).then(() => {
showNotification('内容已复制到剪贴板', 'success');
});
}
// 处理导出
function handleExport() {
const textToExport = outputArea.textContent || outputArea.innerText;
const blob = new Blob([textToExport], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `jsp-output-${new Date().getTime()}.txt`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
showNotification('内容已导出', 'success');
}
// 切换主题
function toggleTheme() {
document.body.classList.toggle('dark');
const icon = themeToggle.querySelector('i');
if (document.body.classList.contains('dark')) {
icon.className = 'fas fa-sun text-yellow-500';
document.body.className = 'bg-gradient-to-br from-gray-800 to-gray-900 min-h-screen';
} else {
icon.className = 'fas fa-moon text-gray-600';
document.body.className = 'bg-gradient-to-br from-blue-50 to-indigo-100 min-h-screen';
}
}
// 更新字体大小
function updateFontSize() {
outputArea.style.fontSize = `${fontSize.value}px`;
}
// 应用设置
function applySettings() {
const savedFontSize = localStorage.getItem('jspOutputFontSize');
if (savedFontSize) {
fontSize.value = savedFontSize;
outputArea.style.fontSize = `${savedFontSize}px`;
updateFontSize();
}
// 显示通知
function showNotification(message, type) {
const notification = document.createElement('div');
notification.className = `fixed top-4 right-4 px-6 py-3 rounded-lg shadow-lg z-50 fade-in ${
type === 'success' ? 'bg-green-500 text-white' : 'bg-red-500 text-white'
}`;
notification.textContent = message;
document.body.appendChild(notification);
setTimeout(() => {
notification.remove();
}, 3000);
}
// HTML转义
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "amp;" .replace(//g, ">" .replace(/", """ .replace(/'/g, "039;" }
// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', init);