小编Ala*_*ore的帖子

Junit测试Spring MVC 4控制器的案例,以检查@RequestMapping中的@PathVariable参数

我有以下控制器代码,我必须编写JUnit测试用例.

public class EquipmentController {

    private Map<String, Equipment> equiList = new HashMap <String,Equipment>();

    @RequestMapping("/rest/equipment/{Number}")
    public Equipment getEquipment(@PathVariable String Number){

        if(!equiList.containsKey(Number)){
            lNumber = DEFAULT;
        }
        return equiList.get(Number);

    }
}
Run Code Online (Sandbox Code Playgroud)

我正在编写JUnit测试用例,如下所示:

import static org.springframework.test.web.ModelAndViewAssert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({/* include live config here
    e.g. "file:web/WEB-INF/application-context.xml",
    "file:web/WEB-INF/dispatcher-servlet.xml" */})
public class EquipmentControllerTest {

    @Inject
    private ApplicationContext applicationContext;

    private MockHttpServletRequest request;
    private MockHttpServletResponse response;
    private HandlerAdapter handlerAdapter;
    private EquipmentController controller;

    @Before
    public void setUp() {
       request = new MockHttpServletRequest();
       response = new MockHttpServletResponse();
       handlerAdapter = applicationContext.getBean(HandlerAdapter.class);
       // Get …
Run Code Online (Sandbox Code Playgroud)

java junit spring spring-mvc

13
推荐指数
1
解决办法
2万
查看次数

更新后,Spring MVC将无法显示正确的数据库值

在我的控制器中,将MySQL数据库中的值放入ModelAndView object

有一个单独的程序更新表,MVC应该获取该值,因此没有表单来更新该表.

表被更新,而当我打刷新浏览器,该值将不会在页面上更新.

调节器

@SuppressWarnings("unchecked")
@Secured({ "ROLE_USER", "ROLE_ADMIN" })
@RequestMapping(value = { "/", "/welcome" }, method = RequestMethod.GET)
public ModelAndView defaultPage(@ModelAttribute("user") User user) {
    Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>) SecurityContextHolder
            .getContext().getAuthentication().getAuthorities();
    ModelAndView view = new ModelAndView("/hello");
    // Redirects to admin page if user has admin role
    if (authorities.toString().contains("ROLE_ADMIN")) {
        return new ModelAndView("redirect:/admin");
    }
    /////
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    String userName = auth.getName();
    User userInfo = userDAO.getUserInfo(userName);
    System.out.println("Here's the thing " + …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc

11
推荐指数
1
解决办法
1074
查看次数

返回null而不是控制器中设置的值时,Modelbinder默认为0

我有一个自定义模型绑定器,用于REST API,如下所示:

public class CustomQueryModelBinder : IModelBinder
{
    public Task<ModelBindingResult> BindModelAsync(ModelBindingContext bindingContext)
    {
      if (!String.IsNullOrWhiteSpace(bindingContext.ModelName) && bindingContext.ModelType == typeof(short) && bindingContext.ValueProvider.GetValue(bindingContext.ModelName) != null)
        {
            short value;
            var val = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).FirstValue as string;

            if (String.IsNullOrWhiteSpace(val))
            {
                return ModelBindingResult.SuccessAsync(bindingContext.ModelName, val);
            }
            else if (Int16.TryParse(val, out value) && value >= 0)
            {
                return ModelBindingResult.SuccessAsync(bindingContext.ModelName, value);
            }
            else
            {
                bindingContext.ModelState.AddModelError(bindingContext.ModelName, "The value is invalid.");
            }
        }

        return ModelBindingResult.FailedAsync(bindingContext.ModelName);
    }
}
Run Code Online (Sandbox Code Playgroud)

并且在URI中未指定自定义值的情况下,它应默认为有效值(大于0),但它始终默认为0,即使控制器如下所示:

public async Task<IActionResult> GetAsync(
        [ModelBinder(BinderType = typeof(CustomQueryModelBinder))]short value = …
Run Code Online (Sandbox Code Playgroud)

asp.net-core-mvc dnx asp.net-core

7
推荐指数
1
解决办法
264
查看次数

为什么我的Laravel模块中出现"未找到类"错误?

我正在使用"laravel/framework":"4.2.*"版本,我想将模块系统用于我的项目.我已按照本文档中提供的说明进行操作.

我可以使用以下命令创建模块:php artisan modules:create module_name.我在app目录中创建了一个管理模块,并且已经创建了模块的目录结构.

DB::select('some SQL statement')在管理模块中使用控制器的其中一个操作,但是它给了我以下错误:

找不到"App\Modules\Admin\Controllers\DB"类.

为什么找不到这门课呢?

php database laravel laravel-4

3
推荐指数
1
解决办法
1万
查看次数