您好我是Spring MVC的新手,并尝试了一些简单的例子。
我使用archtype" spring-mvc-archtype'在eclipse中创建了一个新的Spring mvc maven项目。
我正在尝试点击网址并使用地图中的@PathParam访问PathVariables <字符串,字符串>
这是我的控制器类
@Controller
@RequestMapping(value="/greet")
public class HomeController {
@RequestMapping(value="/welcome/{countryName}/{userName}")
public ModelAndView test(@PathVariable("userName") String userName, @PathVariable("countryName") String countryName) throws IOException{
ModelAndView mav = new ModelAndView("home");
mav.addObject("mymessage", "Welcome To "+countryName+" , Spring MVC - "+userName);
return mav;
}
@RequestMapping(value="/hi/{cn}/{un}")
public ModelAndView hi(@PathVariable Map<String, String> pathVars){
String countryName = pathVars.get("cn");
String userName = pathVars.get("un");
ModelAndView mav = new ModelAndView("home");
mav.addObject("mymessage", "Hi "+userName+" in "+ countryName);
return mav;
}
}
这是Configuration类
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="org.gyanbang.kiran.SpringMvc")
public class MvcConfiguration extends WebMvcConfigurerAdapter{
@Bean
public ViewResolver getViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
所以,当我跑步并击中时 http://localhost:8080/SpringMvc/greet/welcome/India/user1它工作正常,我得到了理想的结果。
但在http://localhost:8080/SpringMvc/greet/hi/India/user1上,它会在消息中出现400错误 &#39; 客户发送的请求在语法上不正确。&#39;
我尝试了一些选项,并尝试将spring-webmvc版本更改为4.1.6.RELEASE但在这种情况下它给了我404.
任何帮助都会很棒。 提前谢谢。
答案 0 :(得分:1)
@PathVariable
可以使用Map<String,String>
,您需要将<mvc:annotation-driven/>
用于ApplicationContaxt.xml
。你可以在下面找到代码:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<mvc:annotation-driven/>
答案 1 :(得分:1)
从一开始我真的很想知道为什么你的解决方案不起作用。
我已经检查了收集到地图中的路径变量 - 它们正常工作。我认为您的问题在于调度程序servlet。您可以尝试配置一个或搜索一些现有的解决方案,我建议使用弹簧盒中的解决方案。
请在git上查看我的解决方案。注意build.gradle
文件
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
...
dependencies {
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
runtime('org.springframework.boot:spring-boot-devtools')
}
Link to my git project solution
您的代码行很少,所以应该清楚。但如果事情不明确 - 请不要犹豫,并在评论中提问。
答案 2 :(得分:0)
根据Spring文档:
@PathVariable参数可以是任何简单类型,例如int,long,Date等。
(虽然此行为可以是customised)。
此外,您的控制器指定具有两个参数的URI:
@RequestMapping(value="/hi/{cn}/{un}")
但是控制器方法只声明一个:@PathVariable Map<String, String> pathVars
。
如果您的目标是(因为这表明:&#34;尝试一些简单的示例&#34;)来证明使用Spring MVC进行一些前后调用,那么您最初应该保持简单,例如:
@RequestMapping(value="/hi/{cn}/{un}")
public ModelAndView hi(@PathVariable String cn, @PathVariable String un){
...
}