We need an example for a very beginner, how to make a form with a spring so that there would be one form on the web with a button that just by pressing starts a method, just the start button.
Closed due to the fact that the essence of the question is not clear to the participants by Denis , user207618, rdorn , user194374, Streletz 18 Aug '16 at 6:05 .
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
- Specify the question, what exactly are the problems? - Kirill Stoianov
- netbeans.org/kb/docs/web/quickstart-webapps-spring_en.html step by step instructions. You need to create a form in the jsp file that will send a request to one of the Spring controllers. And in the request handler you and run the method that you wish. - DimXenon
- @KirillStoianov is that after the usual java in the spring it is difficult to see and use and I don’t know English) I found a couple of examples like javastudy.ru/spring-mvc/hello-world-example but the value is being transmitted but I don’t I want to transfer, I just need to add a face to the code that has a web to click on the button and the run () method will start without any extra classes and data transfers. - Ilya
2 answers
The Spring website describes in great detail how it can be done.
Extremely simple maven project for an extremely simple example of form processing using Spring MVC
\ pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>spring-mvc-simple-form</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>Spring MVC Simple Form</name> <properties> <jdk.version>1.8</jdk.version> <spring.version>4.3.2.RELEASE</spring.version> <jstl.version>1.2</jstl.version> <servletapi.version>2.5</servletapi.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>${servletapi.version}</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> </configuration> </plugin> <plugin> <!-- Для простого тестирования --> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.11.v20160721</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <webApp> <contextPath>/</contextPath> </webApp> </configuration> </plugin> </plugins> </build> </project>
\ src \ main \ webapp \ WEB-INF \ web.xml
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Spring MVC Simple Form</display-name> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
\ src \ main \ webapp \ WEB-INF \ mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.example" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
\ src \ main \ java \ com \ example \ SimpleFormController.java
package com.example; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.ui.ModelMap; @Controller @RequestMapping("/hello") public class SimpleFormController { @RequestMapping(method = RequestMethod.GET) public String showForm() { return "form"; } @RequestMapping(method = RequestMethod.POST) public String handleForm(@RequestParam("name") String name, ModelMap model) { model.addAttribute("name", name); return "form"; } }
\ src \ main \ webapp \ WEB-INF \ jsp \ form.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta charset="utf-8"> </head> <body> <form method="post"> <input type="text" name="name" placeholder="Введите ваше имя"> <input type="submit" value="Отправить"> </form> <c:if test="${not empty name}"> <hr> <h3>Привет, ${name}!</h2> </c:if> </body> </html>
You mvn jetty:run
command mvn jetty:run
from the project root and can go to the browser at http://127.0.0.1:8080/hello
- and how to connect to the existing context? just och novice in spring straight at all. There is now written aplication context which in the lane is taken using FileSystemXmlApplicationContext - Ilya
- one@Ilya, for a beginner, a systematic approach to learning is important. To mess in my head was not formed. Moreover, Spring is not a field of knowledge that can be explained on the fingers. I recommend to get Craig Walls' book "Spring in action", leisurely to read and experiment with examples. - Sergey Gornostaev