我有一个网络应用http://localhost:8080/TestWeb/ 我想在我的spring mvc控制器中获取url和数据 例如
http://tech.bg7.com/college/home.html?sn=S1&token=01535141444B88
我的弹簧控制器样品
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@ModelAttribute("login") Users usr,
BindingResult result, Model model, HttpSession session,
HttpServletRequest request) {
//business logic
return "login";
}
调度-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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:component-scan base-package="com.tech.testWeb.*" />
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/*"
cache-period="31556926"/>
<import resource="classpath:springmvc-resteasy.xml"/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
如何从tes.bg7.com服务器获取请求URL到spring mvc控制器?
提前致谢
答案 0 :(得分:1)
使用@RequestParam从url获取数据。 请尝试以下代码。
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@RequestParam("sn")String sn,@RequestParam("token")Integer token)
{
//business logic
}
答案 1 :(得分:0)
将@Vaibs的答案扩展到提取网址:
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login1(@RequestParam("sn")String sn,@RequestParam("token")Integer token,HttpServletRequest request)
{
String url = request.getRequestURL().toString() + "?" + request.getQueryString();
//business logic
}