Tag: spring mvc

将json发布到spring mvc控制器

控制器签名(我也尝试过请求者): @RequestMapping(value = “/Lame”, method = RequestMethod.POST) public @ResponseBody boolean getLame(@RequestParam String strToMatchA, @RequestParam String strToMatchB) {} 这是我的json: { “strToMatchA”: “EN”, “strToMatchB”: “lon” } 不工作,我收到错误: org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter ‘strToMatchA’ is not present 从方法签名中删除第一个参数然后使其工作(方法被正确调用),我该怎么办? 当我更改要使用@RequestBody注释的方法参数时,我收到以下错误: java.io.IOException: Stream closed

Spring安全角色层次结构无法使用Java Config

首先,我是Java Spring Framework的新手。 如果我没有提供足够的信息,请原谅我。 我试图将RoleHierarchy添加到我的应用程序中,但它不起作用。 以下是我尝试过的代码。 SecurityConfig.java // These config is try to set up a user Role Hierarchy @Bean public RoleHierarchy roleHierarchy() { System.out.println(“arrive public RoleHierarchy roleHierarchy()”); RoleHierarchyImpl r = new RoleHierarchyImpl(); r.setHierarchy(“ROLE_ADMIN > ROLE_STAFF”); r.setHierarchy(“ROLE_STAFF > ROLE_USER”); r.setHierarchy(“ROLE_DEVELOPER > ROLE_USER”); r.setHierarchy(“ROLE_USER > ROLE_GUEST”); return r; } @Bean public AffirmativeBased defaultAccessDecisionManager(RoleHierarchy roleHierarchy){ System.out.println(“arrive public […]

Spring用文件名中的点(s)提供静态内容

我想通过Spring提供npm构建的网页,一切都运行正常,但无论真正的后缀是什么(css,js或html),我都无法提供像main.xxxx.yyy这样的资源。 目录树是这样的: src/main/resource/resource index.html asset-manifest.json favicon.ico manifest.json service-worker.js static css main.fc656101.css main.fc656101.css.map js main.91794276.js main.91794276.js.map media banner.bdcf92f4.jpg fontawesome-webfont.912ec66d.svg … 这是应用程序类: @SpringBootApplication public class Application { private static Logger log=Logger.getLogger(Application.class.getName()); @Bean WebMvcConfigurer configurer () { return new WebMvcConfigurerAdapter() { @Override public void addResourceHandlers (ResourceHandlerRegistry registry) { registry.addResourceHandler(“/resources/static/*”). addResourceLocations(“classpath:/static/”); } @Override public void configurePathMatch(PathMatchConfigurer configurer) { super.configurePathMatch(configurer); […]

Ajax请求 – 在Spring Controller上调用不同的方法

关于在Spring MVC中使用AJAX我一直有问题。 我有一个包含很多字段的表单,每个字段根据单击的相关按钮检索数据。 因此,我的每个按钮都需要调用一个AJAX请求。 每个响应都将显示在相关字段中。 我想知道在点击其他按钮后是否可以在我的Spring控制器中调用不同的方法? 换句话说,我想向同一个控制器发出多个ajax请求,其中每个请求将在同一个控制器中调用不同的方法。 看这个例子: // when get account detail is clicked it will call this method @RequestMapping(method=RequestMethod.POST) public @ResponseBody String getAccountDetails(@RequestParam(value=”accountid”) String accountid){ return somefunct.getAccountDetails(accountid); } // when get account summary is clicked it will call this method @RequestMapping(method=RequestMethod.POST) public @ResponseBody String getAccountSummary(@RequestParam(value=”accountid”) String accountid){ return somefunct.getAccountSummary(accountid); } /* when submit […]

如何从Spring MVC中的请求参数中修剪空格

我很难相信我需要从请求参数中修剪前导空格和尾随空格并不是一个常见的要求,但我似乎找不到任何能够很好地解决它的人。 当然,有一些简单的方法可以配置,例如,Spring WebDataBinder在将它们映射到目标对象之前从参数值中修剪所有空格? 我真的很想知道其他人是如何解决这个问题的。

Spring Framework,启用PUT方法

我遇到了捕获发送到服务器的PUT请求的问题。 这些是我的方法: @RequestMapping(method= RequestMethod.GET) public String getCity(@PathVariable(value=”cid”) String cid, @RequestParam(value=”State”) Integer state, Model model) { System.out.println(“get request”); return “index”; } @RequestMapping(method= RequestMethod.PUT) public String putCity(@PathVariable(value=”cid”) String cid, @RequestParam(value=”State”) Integer state, Model model) { System.out.println(“put request”); return “index”; } 当我跟踪调用时,我的PUT请求是由GET方法处理的,而不是我class级中的PUT方法..在屏幕上,它总是读作“获取请求”。 我检查了浏览器日志并确认他们发送了正确的PUT请求,所以我想我在这里错过了一些Spring配置,但我不知道它是什么.. 有人可以帮忙吗? 谢谢。 编辑:附加代码与类: @Controller @RequestMapping(value=”/retail/{cid}/master/city”) public class City { @RequestMapping(value=”/foo1″, method= RequestMethod.GET) public String getCity(@PathVariable(value=”cid”) […]

Spring Security – 白名单IP范围

我查看过的很多资源和stackoverflow问题都提供了使用.xml文件的答案: 使用Spring Security的IPfilter Spring Security 4 Method security using @PreAuthorize,@PostAuthorize, @Secured, EL http://docs.spring.io/spring-security/site/docs/3.0.x/reference/appendix-namespace.html#nsa-gms 我想知道的是,如果可以在不使用XML配置的情况下使用Spring Security将IP地址范围列入白名单? 以下是我的控制器中的一个简单方法: @RequestMapping(value = “/makeit”, method = RequestMethod.GET) @ResponseBody //@PreAuthorize(“hasIpAddress(‘192.168.0.0/16’)”) public String requestData() { return “youve made it”; } 我已经为安全配置创建了一个单独的类,但它没有太多,我只是为EnableGlobalMethodSecurity注释创建它 – 这样我就可以使用@PreAuthorize注释(来自这里的答案: @PreAuthorize注释不工作弹簧安全性 )。 @Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) public class SpringConfiguration extends WebSecurityConfigurerAdapter { @Override public void configure(HttpSecurity http) throws […]

将Java对象转换为Json,反之亦然?

我知道JSON对象只不过是String 。 我的问题是我有一个对象Map,我想将其转换为Json格式。 示例: Java Class -> Class Person{ private String name; private String password; private int number; } Java list -> Map<List,List> map=new HashMap<List,List>(); ..and map has Some data filled in it. 我想将该列表转换为 Json Format? 我怎么能实现它? 因为我想通过HttpClient发送它…如果不是另一种替代方式呢? 据我所知,有Gson API可用,但我不知道如何使用它或以其他有效的方式。 谢谢

Spring-Hibernate使用多个数据源/数据库

我正在使用一个使用Spring MVC 3和Hibernate的Web应用程序 我想为我的Web应用程序使用2个数据源MySql和Oracle数据库, 我已经阅读了许多关于“spring-hibernate multiple datasource / database”的教程和解决问题的例子: directjump2java.blogspot.com 堆栈溢出 论坛spring 等等。 但是当我每次运行它时,配置只读取我的第一个数据库配置(MySql)并显示此错误Table ‘db_prod.ksei_lookup_holiday’ doesn’t exist db.prod是我的第一个数据库(MySql)而KSEI_LOOKUP_HOLIDAY是我的第二个数据库(Oracle),则 这是我的spring.xml org.hibernate.dialect.MySQL5Dialect true org.hibernate.dialect.Oracle10gDialect true 这是我的第一个数据库(MySql)的DAO实现 @Repository @Qualifier(value=”sessionFactory”) public class UserDaoImpl extends HibernateDaoSupport implements UserDao{ @Autowired private UserDaoImpl(SessionFactory sessionFactory){ setSessionFactory(sessionFactory); } 这是我的第二个数据库(Oracle)的DAO实现 @Repository @Qualifier(value=”sessionFactorySOAAPP”) public class UpdateKSEIDaoImpl extends HibernateDaoSupport implements UpdateKSEIDao{ @Autowired private UpdateKSEIDaoImpl(SessionFactory sessionFactorySOAAPP){ setSessionFactory(sessionFactorySOAAPP); } […]

如何在jsp中访问ModelMap?

如何在jsp中从ModelMap访问对象,以便可以在其上调用方法? 目前我收到此错误: Syntax error on token “$”, delete this token JSP Java的 @Controller @RequestMapping(“/dashboard.htm”) @SessionAttributes(“data”) public class DashBoardController { @RequestMapping(method = RequestMethod.GET) public String getPage(ModelMap model) { String[] menus = { “user”, “auth”, “menu items”, }; String[] files = { “menu”, “item”, “files”, }; MenuWriter m = new MenuWriter(menus, files); model.addAttribute(“menus”, m); String[] atocs = […]