如何在spring boot中获取ip所有RequestMapping的URL路径列表集

11:52 提问
springMVC4 默认跳转到requestMapping中对应url的jsp去了?
现在的问题:前段发送请求到对应的controller,可以获取到参数,我想把返回数据放到modelMap中来返回给前台。但是前台一直报404,因为它并没有返回数据,而是去跳转页面了。
希望实现的目的:只返回数据给前台
方法进入的controller
spring-mvc配置文件
&?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"&
&!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --&
&mvc:annotation-driven /&
&!-- 静态资源(js、image等)的访问 --&
&mvc:default-servlet-handler/&
&!-- 使用注解的包,包括子集 --&
&context:component-scan base-package="com.sky.controller" /&
&!-- 简单的异常处理 --&
&bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"&
&property name="exceptionMappings"&
&!-- 映射目录为/WEB-INF/jsp/error/upload_error.jsp --&
&prop key="org.springframework.web.multipart.MaxUploadSizeExceededException"&/error/upload_error&/prop&
&/property&
&bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&
&property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/&
&property name="prefix" value="/WEB-INF/pages/"/&
&property name="suffix" value=".jsp"/&
&!-- springmvc文件上传需要配置的节点--&
&bean id="multipartResolver" class="org.springframework.monsMultipartResolver"&
&property name="maxUploadSize" value=""/&
&property name="defaultEncoding" value="UTF-8"/&
&property name="resolveLazily" value="true"/&
web.xml配置文件
&!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"/dtd/web-app_2_3.dtd" &
&web-app xmlns="/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_3_0.xsd"
version="3.0"&
&display-name&Archetype Created Web Application&/display-name&
&description&skyCredit&/description&
&!-- 加载Spring配置文件 --&
&context-param&
&param-name&contextConfigLocation&/param-name&
&param-value&classpath:/config/applicationContext.xml&/param-value&
&/context-param&
&!-- 编码过滤器 --&
&filter-name&encodingFilter&/filter-name&
&filter-class&org.springframework.web.filter.CharacterEncodingFilter&/filter-class&
&async-supported&true&/async-supported&
&init-param&
&param-name&encoding&/param-name&
&param-value&UTF-8&/param-value&
&/init-param&
&filter-mapping&
&filter-name&encodingFilter&/filter-name&
&url-pattern&/*&/url-pattern&
&/filter-mapping&
&!-- 日志记录 --&
&context-param&
&!-- 日志配置文件路径 --&
&param-name&log4jConfigLocation&/param-name&
&param-value&classpath:config/log4j.properties&/param-value&
&/context-param&
&context-param&
&!-- 日志页面的刷新间隔 --&
&param-name&log4jRefreshInterval&/param-name&
&param-value&6000&/param-value&
&/context-param&
&listener&
&listener-class&org.springframework.web.util.Log4jConfigListener&/listener-class&
&/listener&
&!-- Spring监听 --&
&listener&
&listener-class&org.springframework.web.context.ContextLoaderListener&/listener-class&
&/listener&
&!-- Spring MVC配置 --&
&servlet-name&springMVC&/servlet-name&
&servlet-class&org.springframework.web.servlet.DispatcherServlet&/servlet-class&
&!-- 自定义spring mvc的配置文件名称和路径 --&
&init-param&
&param-name&contextConfigLocation&/param-name&
&param-value&classpath:config/spring-mvc.xml&/param-value&
&/init-param&
&load-on-startup&1&/load-on-startup&
&/servlet&
&!-- spring mvc 请求后缀 --&
&servlet-mapping&
&servlet-name&springMVC&/servlet-name&
&url-pattern&/&/url-pattern&
&/servlet-mapping&
&/web-app&
前台的返回结果 404
按赞数排序
@RequestMapping("/userList")
@ResponseBody
@ResponseBody:加上这个注解就可以返回数据到页面,不加这个注解的话就代表是接下来跳转到页面。
您这里url绑定的方法没有返回值啊,void怎么能行,给你一个参考
上面这个empSee是一个jsp页面,直接在empSee.jsp用el表达式取值的啊,你这个又没用ajax,也没说明最终返回的视图,服务器怎么知道你想到哪里,当然报404啦
因为我在spring里加了这个
就是下面代码
&!-- 增加前缀和后缀 Model和ModelAndView一起用时,也是支持JSP JSTL的解析器 --&&!-- 前面自动加斜线 --&&!-- 后面自动加点jsp --&
所以,我的return 后面直接省略jsp了用empSee,就跳转到修改员工页面了
举个例子:
type: "POST",
url: '/CIC/csCollectionList/queryCreateDateImp.do',
data : "record_id="+record_ids+"&&loan_no="+loans_no,
async: false,//同步提交
success: function(data){
var result= $.parseJSON(data);
if(result.flag=="Y"){
$.messager.alert('提示',"成功");
java代码中:
@RequestMapping("/queryCreateDateImp")
@ResponseBody
public String queryCreateDateImp(String loan_no,String record_id){
Map&String,Object& map=new HashMap&String,Object&();
String flag = "Y";
CsRecordImportantList cr = collectionListServiceImp.queryOverdueDetailImpNo(loan_no,record_id);
if(cr!=null){
flag = "Y";
map.put("cr", cr);
} catch (Exception e) {
e.printStackTrace();
map.put("flag", flag);
return JSON.toJSONString(map);
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐博客分类:
1.spring boot拦截器默认有
HandlerInterceptorAdapter
AbstractHandlerMapping
UserRoleAuthorizationInterceptor
LocaleChangeInterceptor
ThemeChangeInterceptor
2.配置spring mvc的拦截器WebMvcConfigurerAdapter
public class WebAppConfig extends WebMvcConfigurerAdapter
3.实现添加拦截器方法
public void addInterceptors(InterceptorRegistry registry){
registry.addInterceptor可以通过此方法添加拦截器, 可以是spring提供的或者自己添加的
4.实例部分
public class WebAppConfig extends WebMvcConfigurerAdapter{
public static void main(String[] args) {
SpringApplication.run(WebAppConfig.class, args);
* 配置拦截器
* @author lance
* @param registry
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new UserSecurityInterceptor()).addPathPatterns("/user/**");
UserSecurityInterceptor代码
public class UserSecurityInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
5.demo地址
浏览 58806
请问一下registry.addInterceptor(new UserSecurityInterceptor()).addPathPatterns("/user/**");& 一句的作用是什么?是真对请求的URl拦截的,只拦截/user/**下的所有路径,其他路径不拦截
请问一下registry.addInterceptor(new UserSecurityInterceptor()).addPathPatterns("/user/**");& 一句的作用是什么?这个是调用自定义的拦截器拦截指定的路径模式。
浏览: 295405 次
来自: 上海
楼主,想问下,我配置的这个拦截器为啥没有生效呢?import
项目跑步起来啊,打印完日志就 Process finished ...
什么都不懂的孩子 写道请问一下registry.addInte ...
1 、2 楼 那句的意思是加入名称为UserSecurityI ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'博客分类:
& 小结下spring mvc中的@RequestMapping的用法。
1)最基本的,方法级别上应用,例如:
&&&
@RequestMapping(value="/departments")
public String simplePattern(){
System.out.println("simplePattern method was called");
return "someResult";
&& 则访问http://localhost/xxxx/departments的时候,会调用 simplePattern方法了
2) 参数绑定
&&
@RequestMapping(value="/departments")
public String findDepatment(
@RequestParam("departmentId") String departmentId){
System.out.println("Find department with ID: " + departmentId);
return "someResult";
& 形如这样的访问形式:
&& /departments?departmentId=23就可以触发访问findDepatment方法了
3 REST风格的参数
&&
@RequestMapping(value="/departments/{departmentId}")
public String findDepatment(@PathVariable String departmentId){
System.out.println("Find department with ID: " + departmentId);
return "someResult";
& 形如REST风格的地址访问,比如:
/departments/23,其中用(@PathVariable接收rest风格的参数
4 REST风格的参数绑定形式之2
&& 先看例子,这个有点象之前的:
@RequestMapping(value="/departments/{departmentId}")
public String findDepatmentAlternative(
@PathVariable("departmentId") String someDepartmentId){
System.out.println("Find department with ID: " + someDepartmentId);
return "someResult";
&& 这个有点不同,就是接收形如/departments/23的URL访问,把23作为传入的departmetnId,,但是在实际的方法findDepatmentAlternative中,使用
@PathVariable("departmentId") String someDepartmentId,将其绑定为
someDepartmentId,所以这里someDepartmentId为23
5 url中同时绑定多个id
&
@RequestMapping(value="/departments/{departmentId}/employees/{employeeId}")
public String findEmployee(
@PathVariable String departmentId,
@PathVariable String employeeId){
System.out.println("Find employee with ID: " + employeeId +
" from department: " + departmentId);
return "someResult";
&& 这个其实也比较好理解了。
6 支持正则表达式
&&
@RequestMapping(value="/{textualPart:[a-z-]+}.{numericPart:[\\d]+}")
public String regularExpression(
@PathVariable String textualPart,
@PathVariable String numericPart){
System.out.println("Textual part: " + textualPart +
", numeric part: " + numericPart);
return "someResult";
&& 比如如下的URL:/sometext.123,则输出:
Textual part: sometext, numeric part: 123.
&
浏览 138604
对Spring MVC入门的同学不错,公司基本上同事都没用过Spring MVC,其实很想这个东西在公司普及,对开发,不管是效率还是和Spring的结合都是一大进步啊。。。另外,就例如@RequestMapping 这种东西,确实太好用了。用过grails没有呢
@RequestMapping(value = "to_upload") public ModelAndView toUpload() {
return new ModelAndView("background/fileOperate/upload"); }这个用法是要怎么解释呢?请求的值是to_upload,返回的视图是background/fileOperate/upload
对Spring MVC入门的同学不错,公司基本上同事都没用过Spring MVC,其实很想这个东西在公司普及,对开发,不管是效率还是和Spring的结合都是一大进步啊。。。另外,就例如@RequestMapping 这种东西,确实太好用了。我已经在我们公司普及开了,,,,
谢谢lz 学习了 好像不能收藏一样的问题,还是个技术活,用chrome查看源代码,找到收藏的链接,打开地址就可以收藏。
对Spring MVC入门的同学不错,公司基本上同事都没用过Spring MVC,其实很想这个东西在公司普及,对开发,不管是效率还是和Spring的结合都是一大进步啊。。。另外,就例如@RequestMapping 这种东西,确实太好用了。spring mvc不错的,已经超越其他框架了
我给lz一个建议每介绍一个用法的时候你可以先把url写上面下面再写Controller 这样是否更直观这是细节 不喜勿喷 个人看法
浏览: 5405406 次
来自: 广州
有些扩展名为null
非常感谢!!!!!!!!!
https://zhuban.me竹板共享 - 高效便捷的文档 ...
kris_zhang 写道如果有多个@Primary 会怎么样 ...
@PathVariable String department ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'java - Spring Boot - how do I get the base url of a request - Stack Overflow
Join Stack Overflow to learn, share knowledge, and build your career.
or sign in with
I'm running a spring boot and a few of my domain services need to know the domain name.
I know that I could capture it at the application layer in the controller method like this:
@RequestMapping(value="/myMapping",method = RequestMethod.POST)
public ModelandView myAction(HttpServletRequest request) {
OR that, if I were running a traditional web application with a war that I could configure a listener like this:
&listener&
&listener-class&org.springframework.web.context.request.RequestContextListener&/listener-class&
&/listener&
And access it like this:
((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest()
But I do not want to pass around arguments like I would if I captured it at the controller, and I am not running a war with a web.xml.
How can I have my cake and eat it too?
If you use Spring Security, you could store this info in SecurityContext.
The trick is getDetails() method, you can put anything you want in there.
I personally use a custom object that stores basic info I need for the current user.
This example just puts a simple string representing your domain:
import javax.servlet.http.HttpServletR
import javax.servlet.http.HttpServletR
import org.springframework.security.authentication.AbstractAuthenticationT
import org.springframework.security.core.context.SecurityC
import org.springframework.security.core.context.SecurityContextH
import org.
import org.springframework.web.servlet.ModelAndV
import org.springframework.web.servlet.handler.HandlerInterceptorA
@Component
public class DomainInterceptor extends HandlerInterceptorAdapter {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
String domain = // TODO extra domain from request here
SecurityContext context = SecurityContextHolder.getContext();
AbstractAuthenticationToken authentication = (AbstractAuthenticationToken) context.getAuthentication();
authentication.setDetails(domain);
Then to retrieve the domain (for the current request) anywhere in your app you'd do this:
String domain = SecurityContextHolder.getContext().getAuthentication().getDetails().toString();
4,55612038
This is what I did:
import javax.servlet.http.HttpServletR
import org.springframework.util.A
import org.springframework.web.context.request.RequestA
import org.springframework.web.context.request.RequestContextH
import org.springframework.web.context.request.ServletRequestA
public class RequestFetcher {
public static HttpServletRequest getCurrentRequest() {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
Assert.state(requestAttributes != null, "Could not find current request via RequestContextHolder");
Assert.isInstanceOf(ServletRequestAttributes.class, requestAttributes);
HttpServletRequest servletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
Assert.state(servletRequest != null, "Could not find current HttpServletRequest");
return servletR
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabled}

我要回帖

更多关于 spring boot 获取ip 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信