我正在尝试在网址
中传递country
和name
但是在输入上述网址时我收到以下信息:
Jun 21, 2016 1:18:37 AM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/FirstSpringMVCProject/berlin/alex]
in DispatcherServlet with name 'spring-dispatcher'
和错误404
如何在URL中传递多个变量?我只是在URL中使用了一个变量name
来尝试它并且它有效。
弹簧调度-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:context="http://www.springframework.org/schema/context"
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">
<bean id="HandlerMapping"
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean name="/welcome/{countryName}/{userName}" class="com.stack.HelloController" />
<bean name="/hi" class="com.stack.HelloController" />
<!-- <context:component-scan base-package="com.stack" /> -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
HelloWorld课程
package com.stack;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
@RequestMapping(path="/welcome/{countryName}/{userName}", method=RequestMethod.GET)
public ModelAndView helloWorld(@PathVariable(value="userName") String name, @PathVariable(value="countryName") String country) {
ModelAndView model = new ModelAndView("HelloPage");
model.addObject("msg", "Hello " + name +" , you are from " +country );
return model;
}
@RequestMapping("/hi")
public ModelAndView hiWrld() {
ModelAndView model = new ModelAndView("HelloPage");
model.addObject("msg", "Hi world!");
return model;
}
}
HellpPage.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- <title>Insert title here</title> -->
</head>
<h1> First Spring MVC Application Demo </h1>
<h2>${msg}</h2>
<body>
</body>
</html>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>FirstSpringMVCProject</display-name>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:1)
您不需要在dispatcher-servlet.xml中编写这样的bean,因为您已经为它添加了注释。
<bean name="/welcome/{countryName}/{userName}" class="com.stack.HelloController" />
<bean name="/hi" class="com.stack.HelloController" />
没有它你的项目应该可以正常工作。 在您的web.xml文件中,您需要添加
<load-on-startup>1</load-on-startup>
这允许你的servlet容器初始化spring。
答案 1 :(得分:1)
检查您的网址:
但是,您在请求映射中提到了 welcome ,该映射在受尊重的URL中缺失。
因此,没有请求图表可以解决您的网址。
添加 欢迎 到您的浏览器网址路径
那么你的网址将是
请求映射中的http://localhost:8080/FirstSpringMVCProject/welcome/berlin/alex
或删除 welcome 。
然后请求映射将像这样
@RequestMapping(path =“/ {countryName} / {userName}”,method = RequestMethod.GET