想了解Java是否像python和C#mvc一样提供html/jsp扩展方法?的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于javawebpython的相关问题,此外,我们还将为您介绍关于
想了解Java是否像python和C#mvc一样提供html / jsp扩展方法?的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于java web python的相关问题,此外,我们还将为您介绍关于ASP.MVC 重写 JsonResult + 扩展方法 定义统一 JSON 数据、asp.net – MVC 3 htmlhelper的扩展方法来包装内容、asp.net-mvc – ASP MVC Razor视图扩展方法,如何创建“全局”视图方法?、asp.net-mvc – ASP.Net MVC 2.0 Html.HiddenFor HtmlHelper扩展不返回值的新知识。
本文目录一览:- Java是否像python和C#mvc一样提供html / jsp扩展方法?(java web python)
- ASP.MVC 重写 JsonResult + 扩展方法 定义统一 JSON 数据
- asp.net – MVC 3 htmlhelper的扩展方法来包装内容
- asp.net-mvc – ASP MVC Razor视图扩展方法,如何创建“全局”视图方法?
- asp.net-mvc – ASP.Net MVC 2.0 Html.HiddenFor HtmlHelper扩展不返回值
Java是否像python和C#mvc一样提供html / jsp扩展方法?(java web python)
我很早以前就使用过asp.net Mvc Razor视图引擎,但是据我所知,您可以使用jsp来完成Razor视图的所有工作。他们的方法不同,但是您可以用相同的方式来做。此外,还有其他一些模板引擎,例如Thymeleaf
,它们基于jsp,但已将其扩展到可以使用的到达点。顺便说一下,Java中的JSF
和Primefaces
之类的框架与asp.Net Mvc中的Razor视图引擎非常相似。
ASP.MVC 重写 JsonResult + 扩展方法 定义统一 JSON 数据
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
namespace common
{
public class CustomJsonResult: JsonResult
{
/// <summary>
/// 字段对象
/// </summary>
public object data { get; set; }
/// <summary>
/// 对象
/// </summary>
/// <param name="data"></param>
public CustomJsonResult(object data)
{
this.data = data;
}
/// <summary>
/// 自定义消息输出
/// </summary>
/// <param name="context"></param>
public override void ExecuteResult(ControllerContext context)
{
if (context==null)
{
throw new ArgumentNullException("context");
}
var response = context.HttpContext.Response;
response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json";
if (ContentEncoding != null)
{
response.ContentEncoding = ContentEncoding;
}
Data = this.data;
var json = Newtonsoft.Json.JsonConvert.SerializeObject(Data);
response.Write(json);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace common
{
public static class Extensions
{
/// <summary>
/// 扩展返回统一JSON
/// </summary>
/// <param name="data"></param>
/// <param name="msg"></param>
/// <returns></returns>
public static CustomJsonResult Result( this object data,string msg=null)
{
if (msg==null)
{
var datas = new { target = data, code = 200, msg ="成功"};
CustomJsonResult customJsonResult = new CustomJsonResult(datas);
return customJsonResult;
}
else
{
var datas = new { target = data, code = 500, msg=msg };
CustomJsonResult customJsonResult = new CustomJsonResult(datas);
return customJsonResult;
}
}
}
}
/// <summary>
/// 登入接口
/// </summary>
/// <param name="strJson"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Index(string data)
{
return true.Result();
}
返回如下 JSON
asp.net – MVC 3 htmlhelper的扩展方法来包装内容
@html.createLink("caption","url") { <html> content in tags </html> }
结果应该有
<a href="url" title="Caption"> <html> content in tags </html> </a>
任何帮助这个。
解决方法
你可以编写一个完全相同的扩展方法。
这是我刚才写的来演示的。
首先,扩展方法:
public static class ExtensionTest { public static MvcAnchor BeginLink(this HtmlHelper htmlHelper) { var tagBuilder = new TagBuilder("a"); htmlHelper.ViewContext.Writer .Write(tagBuilder.ToString( TagRenderMode.StartTag)); return new MvcAnchor(htmlHelper.ViewContext); } }
这是我们的新类型,MvcAnchor:
public class MvcAnchor : Idisposable { private readonly TextWriter _writer; public MvcAnchor(ViewContext viewContext) { _writer = viewContext.Writer; } public void dispose() { this._writer.Write("</a>"); } }
在你的意见中,你现在可以做:
@{ using (Html.BeginLink()) { @Html.Raw("Hello World") } }
结果如下:
<a>Hello World</a>
稍微扩展以处理您的确切要求:
public static MvcAnchor BeginLink(this HtmlHelper htmlHelper,string href,string title) { var tagBuilder = new TagBuilder("a"); tagBuilder.Attributes.Add("href",href); tagBuilder.Attributes.Add("title",title); htmlHelper.ViewContext.Writer.Write(tagBuilder .ToString(TagRenderMode.StartTag)); return new MvcAnchor(htmlHelper.ViewContext); }
和我们的观点:
@{ using (Html.BeginLink("http://stackoverflow.com","The Worlds Best Q&A site")) { @Html.Raw("StackOverflow - Because we really do care") } }
产生结果:
<a href="http://stackoverflow.com" title="The Worlds Best Q&A site"> StackOverflow - Because we really do care</a>
asp.net-mvc – ASP MVC Razor视图扩展方法,如何创建“全局”视图方法?
我正在尝试创建一些我想直接在视图中可用的方法。这些不是真正的Html帮助方法,所以我不认为扩展HtmlHelper是有道理的吗?
我的目标是能够在视图中调用方法,即
@HelloWorld(); vs @Html.HelloWorld()
我可以通过在HtmlHelper上创建一个扩展方法来让Html.HelloWorld工作
public static class HtmlExtensions { public static string HelloWorld(this HtmlHelper helper) { return "Hello"; } }
我想做同样的事情,但是为了这个观点;我的问题 – 视图是什么类型的对象?
注意:我可以通过在.cshtml页面中定义方法来使其工作
@functions { public string HelloWorld() { return "Hello"; } } @HelloWorld() @* Now this works *@
然后我试图把这个代码我的_viewstart.cshtml文件认为它可以在所有的视图,但它不是
如果我知道哪种类型的视图是我认为可以轻松扩展,任何帮助赞赏
解决方法
using System; using System.Web.Mvc; namespace MyMvcWebApp.Extensions { public abstract class ViewBase<TModel> : System.Web.Mvc.WebViewPage<TModel> where TModel : class { // Now this will be available in any view @HelloWorld() public string HelloWorld() { return "Hello from the ViewBase class"; } } }
这应该使用强类型的视图,它看起来像剃刀所有的视图是强类型的,当你没有定义类型’动态’被使用,这是强类型
同样,当ClickTricity声明你然后更新web.config(在视图目录下的一个)
<pages pageBaseType="MyMvcWebApp.Extensions.ViewBase">
asp.net-mvc – ASP.Net MVC 2.0 Html.HiddenFor HtmlHelper扩展不返回值
ViewData.Model.FooID = <%= ViewData.Model.FooID %>< Model.FooID = <%= Model.FooID %> <%= Html.HiddenFor(x=>x.FooID) %>
但是我们在渲染视图中看到的是:
ViewData.Model.FooID = 515b0403-e75b-4bd7-9b60-ef432f39d338 Model.FooID = 515b0403-e75b-4bd7-9b60-ef432f39d338 <input id="FooID" name="FooID" type="hidden" value="" />
我可以手动添加这个:
<input id="FooID" name="FooID" type="hidden" value="<%= Model.FooID %>" />
但是现在我们不再,但令人惊讶的是,当我这样做时,Html.HiddenFor总是具有正确的值.
解决方法
我知道这个扩展方法有重载使用二进制,但我不确定GUID ….
你有没有试过去补货呢?
今天关于Java是否像python和C#mvc一样提供html / jsp扩展方法?和java web python的分享就到这里,希望大家有所收获,若想了解更多关于ASP.MVC 重写 JsonResult + 扩展方法 定义统一 JSON 数据、asp.net – MVC 3 htmlhelper的扩展方法来包装内容、asp.net-mvc – ASP MVC Razor视图扩展方法,如何创建“全局”视图方法?、asp.net-mvc – ASP.Net MVC 2.0 Html.HiddenFor HtmlHelper扩展不返回值等相关知识,可以在本站进行查询。
本文标签: