使用springboot框架,写一个调查问卷代码
时间: 2023-07-01 12:10:19 浏览: 131
好的,以下是一个简单的调查问卷的代码示例,使用Spring Boot框架和Thymeleaf模板引擎:
1. 创建一个实体类表示问题
```java
@Entity
public class Question {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String content;
private String[] options;
// 省略构造函数、getter和setter方法
}
```
2. 创建一个控制器处理问卷相关请求
```java
@Controller
public class SurveyController {
@Autowired
private SurveyService surveyService;
@GetMapping("/")
public String index(Model model) {
List<Question> questions = surveyService.getAllQuestions();
model.addAttribute("questions", questions);
return "index";
}
@PostMapping("/submit")
public String submit(@RequestParam Map<String, String> answers) {
surveyService.saveAnswers(answers);
return "redirect:/result";
}
@GetMapping("/result")
public String result(Model model) {
Map<String, Integer> stats = surveyService.getStats();
model.addAttribute("stats", stats);
return "result";
}
}
```
3. 创建一个服务类处理问卷相关业务逻辑
```java
@Service
public class SurveyService {
@Autowired
private QuestionRepository questionRepository;
private Map<Long, Question> questionCache;
public SurveyService() {
questionCache = new HashMap<>();
}
public List<Question> getAllQuestions() {
List<Question> questions = questionRepository.findAll();
for (Question question : questions) {
questionCache.put(question.getId(), question);
}
return questions;
}
public void saveAnswers(Map<String, String> answers) {
for (String key : answers.keySet()) {
Long id = Long.parseLong(key);
Question question = questionCache.get(id);
String answer = answers.get(key);
question.getStats().put(answer, question.getStats().getOrDefault(answer, 0) + 1);
questionRepository.save(question);
}
}
public Map<String, Integer> getStats() {
Map<String, Integer> stats = new HashMap<>();
List<Question> questions = questionRepository.findAll();
for (Question question : questions) {
for (String option : question.getOptions()) {
Integer count = question.getStats().get(option);
if (count != null) {
stats.put(option, stats.getOrDefault(option, 0) + count);
}
}
}
return stats;
}
}
```
4. 创建一个模板文件展示问卷页面
```html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Survey</title>
</head>
<body>
<h1>Survey</h1>
<form method="post" action="/submit">
<div th:each="question : ${questions}">
<h2 th:text="${question.content}"></h2>
<div th:each="option : ${question.options}">
<input type="radio" th:name="${question.id}" th:value="${option}"/>
<label th:text="${option}"></label>
</div>
</div>
<button type="submit">Submit</button>
</form>
</body>
</html>
```
5. 创建一个模板文件展示结果页面
```html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Result</title>
</head>
<body>
<h1>Result</h1>
<table>
<tr>
<th>Option</th>
<th>Count</th>
</tr>
<tr th:each="entry : ${stats}">
<td th:text="${entry.key}"></td>
<td th:text="${entry.value}"></td>
</tr>
</table>
</body>
</html>
```
以上是一个简单的调查问卷的代码示例,仅供参考。具体实现方式和细节还需要根据实际需求进行调整。
阅读全文