Fix print rendering for drawing answers

This commit is contained in:
2026-06-03 15:05:11 +08:00
parent 8cd0130691
commit 46f16c37f2
2 changed files with 95 additions and 1 deletions

View File

@@ -170,6 +170,14 @@
.answer-content.wrong {
color: #c00;
}
.drawing-answer-image {
display: block;
max-width: 100%;
max-height: 360px;
margin-top: 6px;
border: 1px solid #ddd;
background: white;
}
.score-get {
color: #c00;
font-weight: bold;
@@ -252,7 +260,7 @@
<div th:each="q : ${data.questions}" class="question-item">
<div class="question-header">
<span class="question-number" th:text="'第' + ${q.exmaid} + '题'">第1题</span>
<span class="question-type" th:text="${q.type == 1 ? '选择题' : '简答题'}">选择题</span>
<span class="question-type" th:text="${q.type == 1 ? '选择题' : (q.type == 3 ? '画图题' : '简答题')}">选择题</span>
<span class="question-score" th:text="${q.score} + '分'">5分</span>
</div>
<div class="question-title" th:text="${q.title}">题目内容</div>
@@ -309,6 +317,25 @@
<span class="score-get" th:text="${q.autoScore} + '/' + ${q.score} + '分'"></span>
</div>
</div>
<!-- 画图题 -->
<div th:if="${q.type == 3}">
<div class="answer-row">
<span class="answer-label">答案:</span>
<span class="answer-content" th:if="${q.userAnswer == null || q.userAnswer.isEmpty()}">未作答</span>
<span class="answer-content" th:if="${q.userAnswer != null && !q.userAnswer.isEmpty()}">
<img th:if="${q.userAnswer.startsWith('data:image')}"
th:src="${q.userAnswer}"
class="drawing-answer-image"
alt="画图题答案">
<span th:unless="${q.userAnswer.startsWith('data:image')}" th:text="${q.userAnswer}"></span>
</span>
</div>
<div class="answer-row" th:if="${data.isCorrected}">
<span class="answer-label">得分:</span>
<span class="score-get" th:text="${q.autoScore} + '/' + ${q.score} + '分'"></span>
</div>
</div>
</div>
</div>
</div>

View File

@@ -0,0 +1,67 @@
package com.baobaot.exam.templates;
import com.baobaot.exam.dto.CorrectDetailItem;
import com.baobaot.exam.dto.CorrectResult;
import org.junit.jupiter.api.Test;
import org.thymeleaf.spring6.SpringTemplateEngine;
import org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.springframework.context.support.GenericApplicationContext;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
class PrintTemplateTest {
private final TemplateEngine templateEngine = createTemplateEngine();
@Test
void rendersDrawingAnswerAsImageOnPrintPage() {
String imageData = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB";
CorrectResult data = CorrectResult.builder()
.answerUid("answer-1")
.studentName("张三")
.examTitle("Java试卷")
.choiceScore(0)
.finalScore(0)
.isCorrected(true)
.questions(List.of(CorrectDetailItem.builder()
.exmaid(1)
.qid(100)
.type(3)
.title("画出流程图")
.score(10)
.userAnswer(imageData)
.autoScore(8)
.build()))
.build();
Context context = new Context();
context.setVariable("data", data);
String html = templateEngine.process("print", context);
assertThat(html).contains("画图题");
assertThat(html).contains("<img");
assertThat(html).contains("src=\"" + imageData + "\"");
assertThat(html).doesNotContain("答案:</span>\n <span class=\"answer-content\">" + imageData);
}
private static TemplateEngine createTemplateEngine() {
GenericApplicationContext applicationContext = new GenericApplicationContext();
applicationContext.refresh();
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setPrefix("classpath:/templates/");
resolver.setSuffix(".html");
resolver.setTemplateMode("HTML");
resolver.setCharacterEncoding("UTF-8");
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(resolver);
return engine;
}
}