#8 -- Add form controller

This commit is contained in:
Anton Romanov 2023-09-06 15:49:59 +04:00
parent f39aa1c2b6
commit 75765558d4
5 changed files with 129 additions and 13 deletions

View File

@ -11,17 +11,16 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
@Configuration
public class MvcConfiguration implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/index");
}
// @Override
// public void addViewControllers(ViewControllerRegistry registry) {
// registry.addViewController("/index");
// }
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

View File

@ -0,0 +1,48 @@
package ru.ulstu.fc.rule.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import ru.ulstu.fc.rule.model.Antecedent;
import ru.ulstu.fc.rule.model.InferenceForm;
import java.util.Arrays;
import java.util.List;
@Controller
public class InferenceMvcController {
@GetMapping("/")
public String initInference(Model model) {
model.addAttribute("ageAntecedents", getAgeAntecedents());
model.addAttribute("incomeAntecedents", getIncomeAntecedents());
model.addAttribute("inferenceForm", new InferenceForm());
return "index";
}
@RequestMapping(value = "get-inference", method = RequestMethod.POST)
public String getInference(@ModelAttribute InferenceForm inferenceForm, Model model) {
model.addAttribute("ageAntecedents", getAgeAntecedents());
model.addAttribute("incomeAntecedents", getIncomeAntecedents());
model.addAttribute("inferenceForm", inferenceForm);
model.addAttribute("response", "123");
return "index";
}
private List<Antecedent> getAgeAntecedents() {
return Arrays.asList(
new Antecedent("молодой", "young"),
new Antecedent("средний", "average"),
new Antecedent("старый", "old"));
}
private List<Antecedent> getIncomeAntecedents() {
return Arrays.asList(
new Antecedent("малый", "small"),
new Antecedent("средний", "average"),
new Antecedent("высокий", "high"));
}
}

View File

@ -1,9 +1,14 @@
package ru.ulstu.fc.rule.service;
package ru.ulstu.fc.rule.model;
public class Antecedent {
private String value;
private String description;
public Antecedent(String description, String value) {
this.description = description;
this.value = value;
}
public String getValue() {
return value;
}

View File

@ -0,0 +1,22 @@
package ru.ulstu.fc.rule.model;
public class InferenceForm {
private String ageAntecedent;
private String incomeAntecedent;
public String getAgeAntecedent() {
return ageAntecedent;
}
public void setAgeAntecedent(String ageAntecedent) {
this.ageAntecedent = ageAntecedent;
}
public String getIncomeAntecedent() {
return incomeAntecedent;
}
public void setIncomeAntecedent(String incomeAntecedent) {
this.incomeAntecedent = incomeAntecedent;
}
}

View File

@ -3,12 +3,54 @@
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}">
<div class="container" layout:fragment="content">
<form action="/get-inference" th:object="${inferenceForm}" method="post">
<div class="row">
Пример вывода по набору нечетких правил:
<br>
</div>
<div class="row">
если возраст средний и доход высокий то размер кредита большой
<br>
</div>
<div class="row">
если возраст молодой и доход высокий то размер кредита средний
<br>
</div>
<div class="row">
если возраст старый и доход средний то размер кредита средний
</div>
<div class="row">
Выберите значения:
</div>
<div class="row">
<div class="col-md-2 col-sm-12">
Возраст
</div>
<div class="col-md-6 col-sm-12">
<select id="select-age-antecedent" class="selectpicker m-2" data-live-search="true"
th:field="*{ageAntecedent}"
data-width="90%">
<option th:each="ageAntecedent : ${ageAntecedents}"
th:value="${ageAntecedent.value}"
th:utext="${ageAntecedent.description}">
</option>
</select>
</div>
</div>
<div class="row">
<div class="col-md-2 col-sm-12">
Доход
</div>
<div class="col-md-6 col-sm-12">
<select id="select-income-antecedent" class="selectpicker m-2" data-live-search="true"
th:field="*{incomeAntecedent}"
data-width="90%">
<option th:each="incomeAntecedent : ${incomeAntecedents}"
th:value="${incomeAntecedent.value}"
th:utext="${incomeAntecedent.description}">
</option>
</select>
</div>
</div>
<input type="submit" class="btn btn-outline-success m-2" value="Получить результат вывода"/>
<div class="row" th:text="${response}"></div>
</form>
</div>
</html>