在Yii中,当访问时<app-host>/index.php?r=<mycontroller>/<myaction>,框架将开始运行代码protected/controller/<MyController>Controller.php
我希望该代码位于另一个文件夹中protected/anotherFolder,而其他控制器保持原样.怎么做?
问候
在Rails 3.1应用程序中,我有一个控制器使用以下代码在索引视图中返回一组对象(子):
def index
@children = Child.all
@base_class = "children-index"
@title = "Your Children"
respond_to do |format|
format.html # children/index.html.erb
format.json { render :json => @children }
end
end
Run Code Online (Sandbox Code Playgroud)
index.html.erb视图的编写方式如下:
<h1><%= title %></h1>
<ul>
<%= @children.each do |child| %>
<li><%= link_to child.fullname, child_path(child) %></li>
<% end %>
</ul>
Run Code Online (Sandbox Code Playgroud)
出于某种原因,JSON响应被抛入HTML响应中,我无法确定原因.我的其他索引视图都没有出现此问题,并且它们的代码非常接近.
John Jake Smith Jr
Jane Ann Doe
[#<Child id: 1, firstname: "John", middlename: "Jake", lastname: "Smith", suffix: "Jr", gender: "Male", dob_actual: "2011-01-05", dob_expected: "2011-01-01", height: 30, weight: 40, created_at: "2011-10-28 21:32:54", …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的艺术品建立一个在线展示/商店,我开始时遇到了一些麻烦.经过朋友的一些建议,我决定使用rails.
我的一个问题是关于控制器.每页都需要一个控制器吗?
截至目前,我计划在我的HTML中有一个Home,Contact,Products和About Us页面.这需要我有四个不同的控制器吗?
如果您有任何意见和建议,请告诉我.
我有一个与Model-View-Controller上的一些C#实现相关的问题.我是C#的新手,所以我很感激一些解释.
我正在开发一个Windows应用程序.使用Visual Studio构建如下.它使用以下3个类.Program.cs,Controller.cs和mainWindow.cs.当应用程序启动时,我想创建一个创建mainWindow的控制器,然后控制器应该处理在mainWindow中输入的所有数据.
Program.cs:
static void main() {
Controller controller = new Controller();
controller.init();
}
Controller.cs
class Controller {
public void init() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new mainWindow(this));
}
}
mainWindow.cs
public partial class mainWindow : Form
{
public mainWindow(Controller parent)
{
InitializeComponent();
parent.closeThisForm();
}
}
Run Code Online (Sandbox Code Playgroud)
因此,当我尝试运行此代码时,我收到以下错误:不一致的可访问性:参数类型'... Controller'比方法'abc.mainWindow.mainWindow(abc.Controllers.mainController)'更难访问.
我知道必须用特权做某事,但不明白为什么和什么.
希望任何人都能帮助我解决这个问题(相当简单的问题).
这是问题:我有一个remoteForm,它本身工作正常.我调试了控制器,每个命令都会运行.但是,如果发生异常并且我设置了flash.error消息.仅在我手动按F5(刷新)后才会显示该消息.我不明白为什么AJAX调用有效,但flash消息是空的.
我有一个远程表单(在index.gsp中):
...
<g:formRemote name="remoteForm"
url="[ controller: 'document',
action: 'search']"
onLoading="showSpinner();"
update="searchBlock"
action="${createLink(controller: 'document', action:'search')}"
>
<div class='info_message'>
${flash.message}
</div>
<div id="searchBlock">will be exchanged</div>
......
Run Code Online (Sandbox Code Playgroud)
控制器:
......
} catch (Exception e){
log.error(e)
flash.error = e.getMessage()
flash.message="HINTTEST"
render (template:'searchNoResult')
return
}
.....
Run Code Online (Sandbox Code Playgroud)
模板(_searchNoResult)具有以下内容:
<span>Sorry, your search yielded no result.</span>
<g:if test='${flash.error}'>
<div class='error_message'>
Error: ${flash.error}
</div>
</g:if>
<g:if test='${flash.messages}'>
<div class='info_message'>
Hint: ${flash.message}
</div>
</g:if>
Run Code Online (Sandbox Code Playgroud)
文档说明:闪存范围通过当前请求和之后的调用.如果我使用普通表单并提交一切正常,但我想使用ajax和渲染模板,所以我不必经常重复代码.
编辑1 首先渲染index.gsp.remoteForm使用包含flash.messages的模板更改seachBlock div.他们没有被填补.
Grails版本1.3.7目前无法升级.
编辑2 异常e,不是Exception,它是我原创的.消息为null,导致消息为null ....
我有CakePHP控制器代码,它抛出以下错误'致命错误:在非对象上调用成员函数create()'.
控制器代码如下:
if ($this->request->is('post')) {
$this->MonthlyReturn->create();
$this->MonthlyReturn->saveField('company_id', $cid); // Assign current company ID to this monthly return before saving
if ($this->MonthlyReturn->save($this->request->data)) {
$this->Session->setFlash(__('The monthly return has been saved'));
$this->redirect(array('action' => 'index'));
} else
{
$this->Session->setFlash(__('The monthly return could not be saved. Please, try again.'));
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
MVC3中的默认模板在登录页面的查询字符串中设置"returnurl"变量.然后该页面回发给控制器
@using (Html.BeginForm()) {
Run Code Online (Sandbox Code Playgroud)
然后在控制器中拾取它就像这样
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
Run Code Online (Sandbox Code Playgroud)
我想在表单中添加一个CSS类,所以我将帮助器更改为:
@using (Html.BeginForm("LogOn", "Account", FormMethod.Post, new { @class = "form-horizontal" }))
Run Code Online (Sandbox Code Playgroud)
但现在查询字符串中的信息未在控制器中设置.
我总是可以在表单中为retrunurl设置一个隐藏的输入值,但我不知道是否有更简单的方法.
谢谢
我目前正在尝试使用位于此处的Miles J插件http://milesj.me/code/cakephp/uploader虽然我在学习CakePhp方面取得了很大进展,但我目前在使用该插件时遇到了问题,我将不胜感激.
我已按照所有必要步骤使用该插件.它已被下载到插件文件夹,我用CakePlugin :: loadAll()引导.
到现在为止还挺好.
接下来,我按照插件开发人员的说明继续设置表.
好的,现在回到我自己的代码.我有以下设置:
images_controller.php,image.php及其视图.
我现在的目标是在这些文件中使用插件:
App::import('Vendor', 'Uploader.Uploader');
Class ImagesController extends AppController {
var $components = array('Auth');
var $helpers = array('Design');
var $uses = array('Image', 'Uploader.Upload');
function manage(){
//here I show a simple upload form that uses the action saveimage
}
function saveimage(){
$this->Uploader = new Uploader();
if(!empty($this->data)){
$this->Upload->save($this->data);
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我的模型设置如下
Class Image extends AppModel {
public $useTable = 'uploads';
public $actsAs = array(
'Uploader.FileValidation' => array(
'file' => array(
'extension' => …Run Code Online (Sandbox Code Playgroud) 我有一个简单的控制器,如下所示,
@Controller
@RequestMapping("/add/*")
public class RequestMappingController {
@Autowired
private MathOps mathOps;
@RequestMapping(value = "add2Operands", method = RequestMethod.GET)
public String add(@RequestParam("op1") String op1, @RequestParam("op2") String op2, Model model) {
int num1 = Integer.parseInt(op1);
int num2 = Integer.parseInt(op2);
int result = mathOps.add(num1, num2);
model.addAttribute("op1", op1).addAttribute("op2", op2).addAttribute("result", result);
return "adder";
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的InteralViewResolver配置
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
adder.jsp文件直接位于/ views文件夹下.
当我导航到
http://localhost:8080/mvc/add/add2Operands?op1=12&op2=2
Run Code Online (Sandbox Code Playgroud)
我收到404错误
/mvc/add/WEB-INF/views/adder.jsp
Run Code Online (Sandbox Code Playgroud)
其中"mvc"是我的上下文路径.
我在这做错了什么?
谢谢
我相信Symfony2的流程如下:请求 - >控制器 - >响应.
实际上,使用此代码,MessageController不返回响应对象,而只返回另一个Controller的值:
MessageController的Compagny/MessageBundle:
<?php
class MessageController extends Controller
{
public function getAction()
{
$message = "I display this message";
return $message; // It's not a new Response object, it returns only a value
}
}
Run Code Online (Sandbox Code Playgroud)
InterfaceController的Compagny/InterfaceBundle:
<?php
class InterfaceController extends Controller
{
public function indexAction()
{
$data = $this->forward('MessageBundle:Message:get');
$response = new Response($data);
return $response; // This one return a Response object
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码是不好的做法吗?