【问题标题】:how to fetch request url from another server into spring mvc controller如何从另一台服务器获取请求 url 到 spring mvc 控制器
【发布时间】:2016-11-05 23:49:22
【问题描述】:

我有一个网络应用程序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 控制器中?

提前致谢

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    使用@RequestParam 从 url 获取数据。 试试下面的代码。

    @RequestMapping(value = "/login", method = RequestMethod.POST)
        public String login(@RequestParam("sn")String sn,@RequestParam("token")Integer token)
        {
        //business logic
        }
    

    【讨论】:

      【解决方案2】:

      扩展@Vaibs 答案以获取 URL:

        @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
        }
      

      【讨论】:

        猜你喜欢
        • 2021-08-31
        • 1970-01-01
        • 1970-01-01
        • 2013-12-09
        • 1970-01-01
        • 2012-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多