Обучение и трудоустройство Java-программистов от Junior до Senior

Обработка несеольких форм на странице

0
13 сен 2017 16:15
Добрый день. есть index.jsp в нем 2 spring:form. Первый select, второй для регистрации, 1 спринговый сервлет и контроллер. Проблема в том что обработка форм мешают друг другу, как можно 1 сервлетом (потому что второй создавать не вижу смысл все равно обработка того же URL) обработать 2 или более форм по одному URL

Ответов: 2

0
14 сен 2017 09:13
У меня формы спринг 1 селект список студентов, при выборе во второй селект получаю список его занятий. 2 Регистрация нового студента.
Сама страница
[HTML]<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"; %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"; %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"; %>

<c:set var="contextPath" value="${pageContext.request.contextPath}"/>

<html>
<head>

<link href="${contextPath}/resources/css/bootstrap.min.css" rel="stylesheet">
<link href="${contextPath}/resources/css/common.css" rel="stylesheet">

<link integrity="import" href="additem.jsp">

<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>;
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>;



<title>Main</title>
</head>
<body>

<form:form method="POST" modelAttribute="selectStudent">
<div class="container">
<form:select path="id" onchange="this.form.submit()" >
<form:option value="0" label="--- Select ---" />
<form:options items="${studentsList}" itemValue="id" itemLabel="studentName"/>
</form:select>
</div>
</form:form>

<form:form method="POST" modelAttribute="selectSection">
<div class="container">
<form:select path="sectionName">
<form:option value="NONE" label="--- Select ---" />
<form:options items="${sectionsList}" itemValue="sectionName" itemLabel="sectionName"/>
</form:select>
</div>
</form:form>




<form:form action="/reg" method="POST" modelAttribute="studentForm" class="form-signin">
<h2 class="form-signin-heading">Create your account</h2>
<div class="form-group">
<form:input type="text" path="studentName" class="form-control" placeholder="Studentname"></form:input>
</div>

<div class="form-group">
<form:input type="text" path="studentLastName" class="form-control" placeholder="StudentLastName"></form:input>
</div>

<button class="btn btn-lg btn-primary btn-block" type="submit">Submit</button>
</form:form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>;
<script src="${contextPath}/resources/js/bootstrap.min.js"></script>

</body>
</html> [/HTML]

Controller

[JAVA] package am.david.jcl.controller;

import am.david.jcl.model.Section;
import am.david.jcl.model.Student;
import am.david.jcl.service.SectionService;
import am.david.jcl.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.annotation.WebServlet;

/**
* Created by David on 9/9/2017.
*/
@Controller
public class PageController {

@Autowired
private StudentService studentService;

@Autowired
private SectionService sectionService;

@RequestMapping(value = "/", method = RequestMethod.GET)
public String selectStudent (Model model) {

Student student = new Student();
model.addAttribute("selectStudent", student);
model.addAttribute("studentsList", studentService.findAll());

Section section = new Section(studentService.findAll());

model.addAttribute("selectSection", section);
model.addAttribute("sectionsList", student.getSections());

return "main";
}

@RequestMapping(value = "/", method = RequestMethod.POST)
public String selectStudent(@ModelAttribute("selectStudent") Student selectStudent, Model model){

model.addAttribute("studentsList", studentService.findAll());

Section section = new Section(studentService.findAll());

model.addAttribute("selectSection", section);

for (Student students: section.getStudents()) {

if (students.getId().equals(selectStudent.getId())) {
model.addAttribute("sectionsList", students.getSections());
}

}

return "main";
}

@RequestMapping(value = "/main", method = RequestMethod.GET)
public String addStudent (Model model) {

model.addAttribute("studentForm", new Student());

return "main";
}

@RequestMapping(value = "/main", method = RequestMethod.POST)
public String addStudent(@ModelAttribute("studentForm") Student studentForm, Model model){

studentService.save(studentForm);

return "main";
}


} [/JAVA]

И Сервлет
[JAVA] <servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>


<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/List/*</url-pattern>
</servlet-mapping> [/JAVA]
0
14 сен 2017 05:27
<input type="hidden" name="form-type" value="select-form" />
Модераторы: Нет
Сейчас эту тему просматривают: Нет