我想傳遞一個country
和name
在URL傳遞多個變量來控制
但我正在逐漸輸入上述網址時,以下幾點:
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>
啓用註釋' '。 –
Blank