我正在使用spring mvc 4开发一个小应用程序,它从用户那里获取输入并将其插入到数据库中。 这是dispatcher-servlet.xml
<?xml version='1.0' encoding='UTF-8' ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="controllers.FormController" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
</beans>
这是我的控制器
@Controller
@RequestMapping("/form")
public class FormController {
@Autowired
private FormService formService;
public FormService getFormService() {
return formService;
}
public void setFormService(FormService formService) {
this.formService = formService;
}
@RequestMapping(value = "/userForm", method = RequestMethod.GET)
public String showUser(Model model){
User user = new User();
model.addAttribute("userForm", user);
return "index";
}
@RequestMapping(value = "/adduser", method = RequestMethod.POST)
public String addUser(@ModelAttribute("userForm") User user, BindingResult result, Model model){
formService.insert(user.getUsername(), user.getPassword());
return "success";
}
}
当我在浏览器“http://localhost:8080/FormSpring/form/userForm.htm”中输入以下网址时,虽然成功页面位于WEB-INF / jsp目录中,但它显示找不到页面错误
这是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>
此外,项目的结构如下图所示
答案 0 :(得分:1)
你的控制器被映射到adduser,而不是adduser.htm!同样在浏览器中,您执行GET,而不是POST请求。
答案 1 :(得分:0)
给出控制器的端点,jsp页面
网址应为:http://localhost:8080/FormSpring/form/adduser
确保正确的请求类型POST/GET