I am new to Spring Framework . I am trying to create and submit a basic form, but I am not receiving a 404 error response from the second page.
MainController.java:
@RequestMapping(value = "/dangki", method = RequestMethod.GET)
public ModelAndView student() {
return new ModelAndView("dangki", "command", new Student());
}
@RequestMapping(value = "/addStudent", method = RequestMethod.POST)
public String addStudent(@ModelAttribute("SpringFramework") Student myStudent,
ModelMap model) {
model.addAttribute("sinhvien", myStudent);
return "ketqua";
}
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
">
<context:component-scan base-package="controller"/>
<mvc:annotation-driven/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
dangki.jsp
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Đăng kí</title>
</head>
<body>
<h1>Đăng kí</h1>
<form:form action="/SpringFramework/addStudent.html" method="POST">
Tên: <form:input type="text" path="name" value="sss"/> <br>
ID: <form:input type="number" path="id" value="sss"/> <br>
<input type="submit" value="Đăng kí">
</form:form>
</body>
</html>
ketqua.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
${sinhvien.name}
${sinhvien.id}
</body>
</html>
What am I missing?
请将此行重写为控制器。
@RequestMapping(value = "/SpringFramework/addStudent", method = RequestMethod.POST)
i have this is working. please write this line into controller
@RequestMapping(value = "/country", method = RequestMethod.GET)
public ModelAndView country() {
ModelAndView model = new ModelAndView("countrymaster");
model.addObject("school", school);
}
Supposing that your controller definition is just like this:
@Controller
public class StudentController {
//... your code here
}
You have to change the form to post the data to the request mapping you declared, so:
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Đăng kí</title>
</head>
<body>
<h1>Đăng kí</h1>
<form:form action="/addStudent/SpringFramework" method="POST">
Tên: <form:input type="text" path="name" value="sss"/> <br>
ID: <form:input type="number" path="id" value="sss"/> <br>
<input type="submit" value="Đăng kí">
</form:form>
</body>
</html>
By the way SpringFramework
is a really poor choise for a variable name, I would use student
for example.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.