我有自己的遗传App.Controller距离Mvc.Controller,然后我所有的控制器继承.我写了利用接口的提供者和执行它MyService和构造函数将Server财产的Mvc.Controller这是HttpServerUtilityBase.
但是,我实例化MyService了App.Controller构造函数.问题是ServerController 的属性null在构建时MyService.我曾经习惯于public Controller () : base() { }建造基地.但是,Server遗体null.
我想Web.HttpContext.Current.Server尽可能避免.
有没有人解决这个问题?
编辑:嗯,我已经实现了tvanfosson的建议,当我的app MyService在属性get方法中构造时,Server仍然是null.
编辑2:没关系,我是个傻瓜.我有另一个Controller使用Server,并没有改变它.案件结案.
我的GSP上有一个简单的自动完成文本输入字段,它通过AJAX请求从控制器获取其值(国家列表).如果控制器看起来像这样,一切正常:
def getAllCountries() {
def countries = [[name: 'Afghanistan', code: 'AF'],[name: 'Åland Islands', code: 'AX'],[...
render countries as JSON
}
Run Code Online (Sandbox Code Playgroud)
但是,如果想要以json格式的方式传递已经包含国家/地区的字符串中的国家/地区,那么我可以对Internationalisaion使用不同的字符串表示形式,如下所示:
def countries = "[{'name':'Afghanistan', code: 'AF'},{'name':'Åland Islands', code: 'AX'},{'name':'Albania', code: 'AL'},{..."
Run Code Online (Sandbox Code Playgroud)
但是,现在当我将渲染国家称为JSON时,我收到了一个强制转换异常.
有什么办法可以直接将String传递给视图吗?
非常感谢你.
我试图显示一个块作为响应ajax调用的一部分.一切正常,除了我无法让控制器回显模板HTML代码.
我的模块类:
class MyModule_Ajax_ProductController extends Mage_Catalog_ProductController {
public function indexAction() {
if ($product = $this->_initProduct()) {
echo '<div>hello</div>'; //this works
echo $this->getLayout()->createBlock('ajax/product')->setTemplate('mymodule_ajax/product.phtml')->toHtml();
//I also tried:
//$layout = $this->getLayout();
//$update = $layout->getUpdate();
//$update->load('ajax_product_index');
//$layout->generateXml();
//$layout->generateBlocks();
//$output = $layout->getOutput();
//echo $output;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我的模板文件product.phtml中 - 这个HTML永远不会显示出来.(保存到app/design/frontend/default/default/template/mymodule_ajax/product.phtml)
<div>HERE!</div>
Run Code Online (Sandbox Code Playgroud)
我的块类:
class MyModule_Ajax_Block_Product extends Mage_Catalog_Block_Product
{
private $product;
protected function _construct()
{
parent::_construct();
$this->setTemplate('mymodule_ajax/product.phtml');
}
protected function _toHtml() {
return parent::_toHtml();
}
public function setProduct($product) {
$this->product = $product;
return $this;
}
public function …Run Code Online (Sandbox Code Playgroud) 我想在Laravel 4控制器中制作可测试的代码.我知道DI(依赖注入),我知道可测试代码可能如下所示:
class UsersController extends BaseController
{
public function __construct(User $user, Notice $notice)
{
$this->user = $user;
$this->notice = $notice;
}
public function getIndex()
{
...
$this->user
...
$this->notice
...
}
public function getPage()
{
...
$this->user
...
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我将两个对象注入控制器.
对于getIndex功能,它是完美的,我在其中使用两个对象.
但请注意,对于getPage功能,我只是使用$this->user,我不需要$this->notice.
但是,$this->notice对象已经实例化了,这真的不太好.
有没有更好的方法来不实例化所有对象?
我正在尝试一个AddressBook Angular应用程序的简单示例.我有一个工厂,它返回一个记录数组,它使用List控制器显示在列表视图中
angular.module('abModule', ['ngRoute'])
.factory('AddressBook', function() {
var address_book = [
{
"id": 1,
"first_name": "John",
"last_name": "Doe",
"age": 29
},
{
"id": 2,
"first_name": "Anna",
"last_name": " Smith",
"age": 24
},
{
"id": 3,
"first_name": "Peter",
"last_name": " Jones",
"age": 39
}
];
alert('inside factory function');
return {
get: function() {
return address_book
}
};
})
.config(function($routeProvider) {
$routeProvider
.when('/', {
controller:'list_controller',
templateUrl:'list.html'
})
.when('/add', {
controller:'add_controller',
templateUrl:'add.html'
})
.otherwise({
redirectTo:'/'
});
})
.controller('list_controller',['$scope', function ($scope, AddressBook) {
$scope.address_book …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用此签名测试方法:
@Autowired
HttpSession http_Session;
@RequestMapping(method=RequestMethod.GET, value="/search/findByName")
public @ResponseBody List<Map> search(@RequestParam(value="name", required=true) String name){
Integer user_id = http_Session.getAttribute("userinfo");
}
Run Code Online (Sandbox Code Playgroud)
userinfo是一个类,其中包含有关用户的信息,并在用户登录时设置在会话范围内.但是当我尝试测试时:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {
"classpath:/META-INF/applicationContext.xml"})
public class userControllerTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
}
@Test
public void userTest() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/search/findByName").param("name", "bob"))
.andDo(print())
.andExpect(MockMvcResultMatchers.status().isOk());
}
Run Code Online (Sandbox Code Playgroud)
问题是userinfo类属性是在另一个方法中设置的,所以当我尝试在这个方法中访问它时,我得到了一个NullPointerException,并且通过自动装配httpSession,我为每个必须测试的方法获得了一个新的Session.
我应该如何处理session属性,我的方法不接受会话参数,并且对于每个测试,它都会创建一个带有新会话的WebApplicationContext.
在CakePHP 2中我可以通过使用获取当前操作$this->action,但在CakePHP 3.x中我不能再使用它了,因为它返回以下错误:
Error: actionHelper could not be found.
Run Code Online (Sandbox Code Playgroud)
如何在CakePHP 3中获取当前操作?
我在Spring Boot中相当新,并试图构建一个简单的Web应用程序.我已经定义了一个包含我的url映射的控制器类,但是在浏览器上它给了我一个白标签页错误(404).我无法理解为什么它无法映射.我已经尝试更改我的组件扫描基础包,但它仍然没有将我重定向到页面"abc"或在控制台中打印"控制器".
的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>bms</groupId>
<artifactId>com.wellmanage.bms</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>BMS</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
控制器类
package com.wellmanage.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class BookController {
@RequestMapping("/")
public …Run Code Online (Sandbox Code Playgroud) 因此,如果请求是针对JSON的,那么这三种方法都在Laravel文档中进行测试.
我基本上想要为请求JSON或AJAX的任何请求返回JSON.
Request::ajax()
Request::isJson()
Request::wantsJson()
我可以使用这些吗?它们是否重叠?
在春季启动时发送休息响应的最佳方法是什么?另外我应该如何管理发送状态代码才能正确完成?
目前我使用ResponseEntity,但我怀疑这是最优雅的方式.
示例代码:
@PostMapping()
public ResponseEntity post(@Valid @RequestBody Item item, BindingResult bindingResult){
if (bindingResult.hasErrors()){
return new ResponseEntity<>(new ModelErrors(bindingResult), HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>(itemService.addItem(item), HttpStatus.CREATED);
}
Run Code Online (Sandbox Code Playgroud)
ModelErrors类扩展了HashMap类,只是获取并包装BindingResult的错误消息.
controller ×10
java ×2
laravel ×2
php ×2
spring ×2
ajax ×1
angularjs ×1
asp.net-mvc ×1
attributes ×1
block ×1
c# ×1
cakephp ×1
cakephp-3.0 ×1
class ×1
factory ×1
grails ×1
httpsession ×1
json ×1
junit4 ×1
laravel-4 ×1
magento ×1
pom.xml ×1
response ×1
rest ×1
spring-boot ×1
spring-mvc ×1