标签: controller

注入到 spring 控制器中的服务在其中一项功能中不可用

我正在编写 spring 控制器,它注入一个 bean。

\n\n

bean 添加到配置中(我们使用 java 配置来完成所有操作):

\n\n
@Bean\npublic NotificationService notificationService() {\n    return new NotificationService();\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

该服务本身几乎没有注入的依赖项和功能:

\n\n
public class NotificationService {\n\n    @Inject\n    NotificationRepository notificationRepository;\n\n    @Inject\n    ProjectRepository projectRepository;\n\n    @Inject\n    ModelMapper modelMapper;\n\n    public NotificationDto create(NotificationDto notificationDto) {\n        //convert to domain object, save, return dto with updated ID\n        return notificationDto;\n    }\n\n    public void markAsRead(Long id, String recipientNip) {\n        //find notification, update status\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

模型映射器几乎没有任何配置,只是设置为严格。JpaRepository同时,存储库是没有自定义功能的扩展接口。他们是由 找到的@EnableJpaRepositories

\n\n

最后我有控制器尝试使用上面的代码:

\n\n
@RestController\n@RequestMapping("/notifications")\npublic class NotificationController extends ExceptionHandlerController {\n\n    @Autowired\n …
Run Code Online (Sandbox Code Playgroud)

java rest spring dependency-injection controller

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

表名称中包含句点的表的 DbContext。实体框架

我正在尝试创建dbcontext一个数据库来查询数据,但表名称是TableTable在 SQL Server 中。我没有创建数据库,也无权使用它执行任何操作。

dbcontext如果表中有句点,如何创建一个或一个类?

public DbSet<ADM.INCIDENTS> ADM.INCIDENTS { get; set; }
Run Code Online (Sandbox Code Playgroud)

另外,如果您使用实体框架创建表的类,是否需要命名该类ADM.INCIDENTS.cs?如果你这样做,你会怎么做?

public class ADM.INCIDENTS
{
    [Key]
    public int ID{ get; set; }

    public string Incident{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)

c# sql-server entity-framework controller

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

如何在 Symfony 中获取 Composer 安装的包的版本?

我们有多个供应商捆绑包(外部和内部编写的),它们通过 Composer 安装到 Symfony 中。因此,composer.json 中的一个示例是:

"repoName/ThisBundle" : "dev-release/1.1.2"
Run Code Online (Sandbox Code Playgroud)

那么在 Symfony 的控制器中,我如何请求“ThisBundle”或“repoName/ThisBundle”的版本返回“dev-release/1.1.2”?

其中一个捆绑包为我们所有的应用程序提供了一个模板,并且在其中一个应用程序中我想显示正在使用的模板版本。

controller version symfony composer-php

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

尝试在 Laravel 的控制器存储方法中分配非对象的属性

我是 Laravel 的新手,请帮我解决这个问题

我在控制器中有存储方法并尝试在该方法中设置属性

public function store(Request $request) {

    $configuration = $this->validate(request(), [
      'device_name' => 'required',
      'sn' => 'required'
    ]);

    $configuration->timezone = "+4"; 
    configuration::create($configuration);

    return back()->with('success', 'configuration has been added');
}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试提交请求时出现此错误

尝试分配非对象的属性

我在上面的方法中做错了什么?

php controller store laravel

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

RSpec:NameError:未初始化的常量 &lt;MyControllerName&gt;

我是 RSpec 的新手,我只想测试我的控制器。我这样写了我的测试:

RSpec.describe ServicesController do
  describe "GET index" do
    it "renders the index template" do
      get :index
      expect(response).to render_template("index")
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

但 RSpec 无法识别我的控制器,我收到此错误:

NameError:未初始化的常量 ServicesController

在找到的示例中,我可以找到require行,这也许可以解决这个问题,但据我所知,它只涉及lib文件夹中的文件,而我的控制器路径是app/controllers/services_controller.rb. 我尝试使用 path 添加它../app/controllers/services_controller.rb,从文件夹中出去lib,但这不起作用。我应该怎么办?这是非常基本的情况,但我无法在网上找到任何帮助。

testing controller rspec ruby-on-rails

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

Spring MVC - 重定向后获取模型数据

我想在重定向后将数据传递到视图。例如,我按下一个按钮,它会将我重定向到包含来自控制器的数据的页面。我正在尝试使用每个人都建议的 RedirectAttribute,但我无法让它工作。任何帮助表示赞赏。

索引.jsp:

<a href="user.htm">Display All Users</a>
Run Code Online (Sandbox Code Playgroud)

控制器:

@RequestMapping(value = "/user.htm")
public ModelAndView addUser(RedirectAttributes redirAtt) {
    ModelAndView mv = new ModelAndView("redirect:user");
    String out = "All User Details: ";
    try {
        Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
        List result = session.createQuery("from Users").list();
        mv.addObject("users", result);
        session.getTransaction().commit();

    } catch (Exception e) {
        e.printStackTrace();
    }

    redirAtt.addFlashAttribute("message", out);
    return mv;
}
Run Code Online (Sandbox Code Playgroud)

我想要在上面显示数据的jsp:user.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>${message}</title>
    </head>
    <body>
        <h1>${message}</h1><br>
        <table>
            <tr>
                <th>Username</th>
                <th>Nickname</th>
                <th>Email</th> …
Run Code Online (Sandbox Code Playgroud)

java spring jsp controller spring-mvc

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

如何获取 Symfony 控制器中搜索查询的总计数

用例:我想将搜索查询和结果总数存储到数据库中,这样我就可以看到人们正在搜索的内容在我的应用程序中不存在。

\n\n

什么是有效的:我能够获取并存储查询,但我不知道如何获取搜索结果的总数。

\n\n

这是来自控制器的代码示例。当我当前尝试此操作时,我得到以下信息

\n\n
\n

错误:可捕获的致命错误:类对象\n Knp\\Bundle\\PaginatorBundle\\Pagination\\SlidingPagination 无法\n 转换为字符串

\n
\n\n

我尝试通过请求 $pagination[totalCount] 将 $pagination 像数组一样处理,但只是返回 null。

\n\n
 public function fpcAction(Request $request)\n    {\n\n            $query = dump($request->query->get(\'q\'));\n\n            $finder = $this->container->get(\'fos_elastica.finder.app.product\');\n            $page = $request->query->getInt(\'page\', 1);\n\n            $paginator = $this->get(\'knp_paginator\');\n            $results = $finder->createPaginatorAdapter($query);\n            $pagination = $paginator->paginate($results, $page, 12);\n\n\n\n            $searchmetrics = new SearchTerms();\n            $searchmetrics->setSearchterm($query);\n            $searchmetrics->setDate(time());\n\n            // TODO: Need to get the total qty of search results for this specific query\n            $searchmetrics->setResultsqty($pagination);\n\n            $entityManager = $this->getDoctrine()->getManager();\n            $entityManager->persist($searchmetrics);\n            $entityManager->flush();\n\n\n            return $this->render(\'default/search.html.twig\', [\'searchresults\' => $pagination, …
Run Code Online (Sandbox Code Playgroud)

php controller symfony knppaginator knppaginatorbundle

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

在 laravel 控制器的构造函数或 laravel 控制器的具体函数中注入模型

想象一下,在 laravel 构造函数(PostController)中,我需要注入 3 个模型:User、Post 和 Comment。

我有两种情况:

1)将这 3 个模型全部注入控制器构造函数中,然后我在具体函数中使用此控制器变量,如下所示:$this->post

2)无论何时何地需要(例如index(Post $post)),将这些模型注入具体控制器的功能中;

这两种做法哪个更好?在第一种情况下,在索引函数中我只使用$this->post变量,但我也创建$this->comment变量,$this->user因为我在其他函数中需要它们?但我不需要它们在索引中,所以每当我通过寻址索引特定路由来调用索引时,我都会注入并创建它们。那不是很糟糕吗?那么这些哪个更好呢?

php model-view-controller controller model laravel

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

Laravel 5:如何使用方法名称创建控制器

我是 Laravel 5 的新手,浏览过https://laravel.com/docs/5.8/controllers但找不到任何方法可以使用带有特定方法名称的 php artisan 创建控制器。我尝试了以下选项,但没有一个起作用。

php artisan make:controller HomeControler#index logout
Run Code Online (Sandbox Code Playgroud)

php artisan make:controller HomeControler/index logout
Run Code Online (Sandbox Code Playgroud)

php artisan make:controller HomeController index logout
Run Code Online (Sandbox Code Playgroud)

Laravel 5 有可能实现这一目标吗?如果是,在使用 artisan make 命令创建控制器时是否也可以声明多个函数?

controller laravel-5 laravel-artisan

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

是否可以将多个过滤器应用于 CodeIgniter 4 中的单个路由

我已经尝试了多种方法来实现此目的,但似乎都不起作用,似乎 CodeIgniter 4 无法将多个过滤器应用于单个路由,目前这是我正在尝试的:

提供InfoFilter.php:

<?php namespace App\Filters;

use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Filters\FilterInterface;

class ProvideinfoFilter implements FilterInterface
{
    public function before(RequestInterface $request, $arguments = null)
    {
        echo "pinfo";
        $account_data  = new \App\Libraries\Account_Data; 

        return $account_data->no_info_redirect();
    } 

    public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

访问过滤器.php:

<?php namespace App\Filters;

use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Filters\FilterInterface;

class AccessFilter implements FilterInterface
{
    public function before(RequestInterface $request, $arguments = null)
    {
        $account_data = new \App\Libraries\Account_Data; 

        echo "accessf"; 
        if ($request->uri->getSegment(1) !== …
Run Code Online (Sandbox Code Playgroud)

php controller routes filter codeigniter-4

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