精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
Some times back I was discussing about asp.net MVC tutorials with one of my Java friends. The talk ended up with a fight trying to prove how one technology is better than other in implementing MVC. For whatever reasons good or bad I was trying to prove that Microsoft technology is the best but…hmm,aaahh and ooohh.
The fact is both the technologies have their own way of doing things. So I ended up writing this comparison article which talks about the differences between how MVC is implemented in J2EE as compared to ASP.NET.
有时候我和我的一个Java朋友讨论过asp.net MVC教程。谈话结束时试图证明一种技术在实施MVC方面比其他技术更好。无论出于何种原因,无论好坏,我都试图证明微软的技术是最好的,但是......嗯,唉,哦。
事实是这两种技术都有自己的做事方式。所以我最后写了这篇比较文章,讨论了与ASP.NET相比,在J2EE中实现MVC的方式之间的差异。
To do full justice in terms of comparison I have divided the article in two parts. In the first part we will compare MVC implementation without using framework and tools.
In the second part I will bring up the comparison of MVC using j2EE struts as compared to ASP.NET MVC visual studio template.
You can watch our Java and J2EE design pattern videos on various topics like Model view controller, front controller, intercepting filter, factory patterns and lot more. Do not miss my .NET design pattern videos which covers 24 patterns including MVC, MVP and MVVM.
为了在比较方面做到充分公正,我将文章分为两部分。在第一部分中,我们将比较MVC实现而不使用框架和工具。
在第二部分中,我将使用j2EE struts与ASP.NET MVC visual studio模板进行比较。
您可以在各种主题上观看我们的Java和J2EE设计模式视频,例如模型视图控制器,前端控制器,拦截过滤器,工厂模式等等。不要错过我的.NET 设计模式视频,其中包括24种模式,包括MVC,MVP和MVVM。
This is not for beginners
Well this article is definitely not for beginners, in case you are looking for MVC fundamental articles you can see my MVC implementation using core Http Handler @ MVC using HttpHandler ASP.NET
那么这篇文章绝对不适合初学者,如果你正在寻找MVC基础文章,你可以使用HttpHandler ASP.NET使用核心Http Handler @ MVC看到我的MVC实现
Overall Comparison without framework
So as we all know in MVC the first hit comes to the controller and the controller binds the view with the model and sends it to the end user.
Model, view and controller form the three core pillars for MVC. From 30,000 feet level (That number makes every architect feel good…) below is how these three modules are implemented in each of these technologies:-
因此我们都知道在MVC中,第一个命中来到控制器,控制器将视图与模型绑定并将其发送给最终用户。
模型,视图和控制器构成了MVC的三大核心支柱。从30,000英尺的水平(这个数字让每个建筑师都感觉良好......)下面是这三个模块在每种技术中的实施方式: -
|
J2ee |
ASP.NET |
Model |
Simple Java bean classes with setters and getters. 带有setter和getter的简单Java bean类 |
Simple .NET class with business logic with setter and getters. 简单的.NET类,包含带有setter和getter的业务逻辑。 |
Controller |
Controller is implemented using HttpServlet class. Controller使用HttpServlet类实现。 |
Controller is implemented using the HttpHandler class. Controller使用HttpHandler类实现。 |
View |
Simple JSP pages. 简单的JSP页面。 |
Simple ASPX page. 简单的ASPX页面。 |
Below is simple pictorial representation of how things actually look like. 以下是事物实际外观的简单图示。
In order to do a proper comparison we have taken a common example. In this example we will do the following:- 为了进行适当的比较,我们采用了一个常见的例子。在此示例中,我们将执行以下操作: -
• When the user comes to the web site, the first page he will see is the Index page. So if it’s ASP.NET he will see index.aspx page, if its j2EE he will see index.jsp page. 当用户访问网站时,他将看到的第一个页面是“索引”页面。所以如果是ASP.NET,他会看到index.aspx页面,如果是j2EE,他会看到index.jsp页面。
• Index page is nothing but a simple page which takes username and password and sends a login command to the controller. 索引页面只是一个简单的页面,它接受用户名和密码并向控制器发送登录命令。
• Once the controller gets the login command, he creates the object of the model and checks if the user is proper or not. 一旦控制器获得登录命令,他就会创建模型的对象并检查用户是否正确。
• If the user is proper he sends them to welcome view page or else he redirects them to the error view page. 如果用户是正确的,他将他们发送到欢迎视图页面,否则他将他们重定向到错误视图页面。
Let’s start with the simplest part of MVC i.e. model.
For the above example we will create a simple class called as “User”, this class will have two properties “Username” and “Password”. The client, which for now the controller will set these two properties can call the “IValid” function to check if the user is valid or not.
In J2EE the model is nothing but the Java bean class , in .NET its a simple class with setter and getters. Below is the sample code for the same.
让我们从MVC最简单的部分即模型开始。
对于上面的例子,我们将创建一个名为“User”的简单类,这个类将有两个属性“Username”和“Password”。客户端,现在控制器将设置这两个属性,可以调用“IValid”函数来检查用户是否有效。
在J2EE中,模型只是Java bean类,在.NET中它是一个带有setter和getter的简单类。下面是相同的示例代码。
J2EE Model using Javabean |
ASP.NET Model using .NET class |
public class UserBean public String getPassword() public void setPassword(String password) public String getUsername() public void setUsername(String
public boolean IsValid |
public class User public string Password |
The next important part is the controller. The controller forms the heart of MVC.
To create a controller in J2EE we create a class which inherits from ‘HttpServlet’ class. The logic of the controller is written in the “processrequest” method. You can access the request and response object using the ‘HttpServletRequest’ class and ‘HttpServletResponse’ class.
To create a controller in ASP.NET we implement the “IHttpHandler” class and override the “processrequest” with the controller logic. Below is the simple code of how the controllers are implemented in both the technologies. To Access the request and response object we need to use the context class in ASP.NET while in J2EE its available as the part of function with different objects as shown in the below code snippet.
下一个重要部分是控制器。控制器构成了MVC的核心。
要在J2EE中创建控制器,我们创建一个继承自'HttpServlet'类的类。控制器的逻辑用“processrequest”方法编写。您可以使用'HttpServletRequest'类和'HttpServletResponse'类来访问请求和响应对象。
要在ASP.NET中创建控制器,我们实现“IHttpHandler”类并使用控制器逻辑覆盖“processrequest”。下面是如何在两种技术中实现控制器的简单代码。要访问请求和响应对象,我们需要在ASP.NET中使用上下文类,而在J2EE中它可用作具有不同对象的函数的一部分,如下面的代码片段所示。
J2EE Controller using Servlet |
ASP.NET controller using HttpHandler |
public class LoginServlet extends { protected void throws ServletException, IOException |
public class LoginHandler : } |
In the controller we can get the data from request and response using in both technologies using the below code.
在控制器中,我们可以使用以下代码在两种技术中使用请求和响应来获取数据。
J2EE Taking data from the JSP form |
Taking data from the .aspx page |
String username = |
User obj = new User(); |
We then call the “isValid” function of the model and redirect to welcome page or to the home page depending on if he is a valid user or not. To redirect in J2EE we use the “RequestDispatcher” class and to redirect in ASP.Net we use the “response.redirect” function. 然后我们调用模型的“isValid”函数并重定向到欢迎页面或主页,具体取决于他是否是有效用户。要在J2EE中重定向,我们使用“RequestDispatcher”类,在ASP.Net中重定向,我们使用“response.redirect”函数。
J2EE checking if user is valid and redirecting |
ASP.NET checking if the user id valid and redirecting. |
boolean isValidUser = model.isValid(); |
if (obj.IsValid()) context.Session["Welcome"] = "welcome " +
context.Response.Redirect("Welcome.aspx"); context.Response.Redirect("Error.aspx"); } |
Now that we have created the controller, we need to map the actions or the URL pattern with the controller. In other words when someone sends an action or URL pattern as “Login” we need to ensure that it invokes the appropriate controller.
Mapping of pattern / action to the controller in both technologies is done by using a configuration XML file. In J2EE this configuration file is called as the “Web.xml” file and in ASP.NET it’s called as “Web.config”.
In J2EE in web.xml we first need to map which URL pattern maps with which servlet. For instance you can see in the below web.xml code snippet we have mapped the Login pattern with LoginServlet servlet name.
现在我们已经创建了控制器,我们需要使用控制器映射操作或URL模式。换句话说,当有人将动作或URL模式发送为“登录”时,我们需要确保它调用适当的控制器。
通过使用配置XML文件,在两种技术中将模式/动作映射到控制器。在J2EE中,此配置文件称为“Web.xml”文件,而在ASP.NET中称为“Web.config”。
在web.xml中的J2EE中,我们首先需要映射哪个URL模式映射到哪个servlet。例如,您可以在下面的web.xml代码片段中看到我们使用LoginServlet servlet名称映射了Login模式。
<servlet-name>
LoginServlet
</servlet-name>
<url-pattern>
/Login
</url-pattern>
</servlet-mapping>
Once the pattern is matched with the servlet name, we then need to map the servlet name with the servlet / controller class as shown in the below code snippet. 一旦模式与servlet名称匹配,我们就需要将servlet名称映射到servlet / controller类,如下面的代码片段所示
In ASP.NET the controller or the handler is mapped with the URL pattern using the web.config file. Below is a web.config file simple XML file code snippet which shows how the Login URL pattern is mapped with the httphandler ‘Loginhandler’. 在ASP.NET中,使用web.config文件将控制器或处理程序与URL模式映射。下面是一个web.config文件简单的XML文件代码片段,其中显示了如何使用httphandler“Loginhandler”映射登录URL模式。
<httpHandlers>
<add verb="POST" path="Login" type="MVCAspWithoutFramework.LoginHandler"/>
</httpHandlers>
Below is how the overall config file looks in both technologies.
J2EE Web.XML |
ASP.NET Web.config |
<servlet-name>LoginServlet</servlet- |
<httpHandlers> </httpHandlers> |
The next important part is the view. The view is nothing but a simple page with the form tag and action having the URL pattern.
You can see how the index.jsp and the index.aspx have the action to Login URL pattern. This URL pattern is then mapped in the web.xml and web.config file to the appropriate controller. 下一个重要的部分是观点。视图只是一个带有表单标记的简单页面,而动作具有URL模式。
您可以看到index.jsp和index.aspx如何对Login URL模式执行操作。然后,此URL模式将在web.xml和web.config文件中映射到相应的控制器。
J2EE view index.jsp |
ASP.NET view index.aspx |
<form action="Login" method="post"> </form> |
<form id="form1" runat="server" <asp:TextBox ID="txtUser" </div> |
|
J2EE |
ASP.NET |
Model |
Simple Java bean classes with setters and getters. 带有setter和getter的简单Java bean类 |
Simple .NET class with business logic with setter and getters. 简单的.NET类,包含带有setter和getter的业务逻辑 |
Controller |
Controller is implemented using HttpServlet class. Controller使用HttpServlet类实现。 |
Controller is implemented using the HttpHandler class. Controller使用HttpHandler类实现。 |
View |
Simple JSP pages. |
Simple ASPX page. |
In today’s world no one implements MVC without help of frameworks. So it’s either ASP.NET or j2ee, framework support is a must. Struts 2 has been the most accepted framework in J2ee while in ASP.NET the ASP.NET MVC template is the king. In this article we will compare these frameworks in terms of how they differ in implementation and their positive and negative points. 在今天的世界里没有人在没有框架帮助的情况下实现MVC。所以ASP.NET或j2ee,框架支持是必须的。Struts 2是J2ee中最受欢迎的框架,而在ASP.NET中,ASP.NET MVC模板是王者。在本文中,我们将比较这些框架在实施方面的差异以及它们的积极和消极方面。
In this article we will compare both frameworks using 8 agenda points: -
在本文中,我们将使用8个议程点对两个框架进行比较: -
Do watch our .Net interview questions and answers video from this link .NET interview questions and answers, you can also catch out J2ee Design pattern videos from this link Java J2ee Design pattern.
In case you are not aware of the frameworks you can see the below videos to just get a quick start in both the frameworks
Click here to view simple ASP.NET MVC video which displays a hello world.
Click here to view simple J2EE struts video to teach struts 2 with the help of an example.
如果您不了解这些框架,您可以看到以下视频,以便在两个框架中快速入门
单击此处查看显示hello世界的简单ASP.NET MVC视频。
单击此处查看简单的J2EE struts视频,借助示例教授struts 2。
Before even I start below is a full comparison sheet which gives an overall view of the comparison. The same we will be discussing in more detail as we proceed in the article. 在我开始下面之前是一个完整的比较表,它给出了比较的整体视图。我们将在本文中进行更详细的讨论。
Comparison points |
Microsoft MVC 2 template |
MVC using J2ee framework (Struts 2) |
Template Automation |
In built with Visual studio |
Need to use open source plug in like http://mvcwebproject.sourceforge.net |
Passing data from controller to view. |
New session management techniques like “viewdata” and “tempdata” are introduced. |
Uses the same request object inherently using same request interceptor. |
Readymade folder structure |
The MVC template creates a readymade logical folder structure for view, controllers and model. |
Does not have a logical folder structure like ASP.NET MVC, but can be created manually. |
Strong typed views for intellisense |
Has string typed view support for better intellisense. |
Achieved by using type casting. |
Helper classes |
Helper classes |
Tag libraries |
URL customization and action mapping |
Uses behind code. |
Uses the Struts 2 XML file. |
URL validation |
URL validation can be done easily using regex. |
Currently no inbuilt mechanism but can be achieved by customized coding. |
Security |
Has readymade security attributes by which we can avoid cross site, SQL injection. |
Currently no inherent feature but can be achieved by customized coding. |
比较点 |
Microsoft MVC 2模板 |
使用J2ee框架的MVC(Struts 2) |
模板自动化 |
在Visual Studio中构建 |
需要像http://mvcwebproject.sourceforge.net一样使用开源插件 |
将数据从控制器传递到视图。 |
引入了新的会话管理技术,如“viewdata”和“tempdata”。 |
使用相同的请求拦截器本身使用相同的请求对象。 |
现成的文件夹结构 |
MVC模板为视图,控制器和模型创建现成的逻辑文件夹结构。 |
没有像ASP.NET MVC这样的逻辑文件夹结构,但可以手动创建。 |
智能感知的强类型视图 |
具有字符串类型视图支持更好的intellisense。 |
通过使用类型转换实现。 |
助手类 |
助手类 |
标记库 |
URL自定义和操作映射 |
使用背后代码。 |
使用Struts 2 XML文件。 |
URL验证 |
使用正则表达式可以轻松完成URL验证。 |
目前没有内置机制,但可以通过定制编码实现。 |
安全 |
具有现成的安全属性,通通过它我们可以避免跨站点,SQL注入。 |
目前没有固有的功能,但可以通过定制编码实现。 |
The first thing which caught our eyes is the Microsoft VS IDE which has a nice readymade template from where developers can start. In MVC J2ee framework the core struts framework does not have something inherent as Microsoft MVC has it. 引起我们注意的第一件事是Microsoft VS IDE,它有一个很好的现成模板,开发人员可以从这里开始。在MVC J2ee框架中,核心struts框架没有Microsoft MVC所拥有的固有内容。
Said and done that it does not mean J2EE community is lagging; you can still take help of open source plug-in to get the template from http://mvcwebproject.sourceforge.net. Below is a simple snapshot of MVC web project template.
The only difference it’s not inherent like Microsoft MVC. 说和做到并不意味着J2EE社区滞后; 您仍然可以从开源插件的帮助中获取http://mvcwebproject.sourceforge.net上的模板。下面是MVC Web项目模板的简单快照。
唯一的区别是它不像微软MVC那样固有。
Conclusion: - Definitely Microsoft wins here in terms of more user friendly interfaces and automation due to the MVC template. In j2ee struts framework we need to hunt for a third party plug which will help us to achieve the same kind automation. 结论: -由于MVC模板,微软肯定会在这里赢得更多用户友好的界面和自动化。在j2ee struts框架中,我们需要寻找第三方插件,这将有助于我们实现同样的自动化。
In MVC one of the most crucial parts is transferring data from controller to view. Microsoft MVC introduces a new variable called as ‘ViewData’ which will help us to transport data between controller and view as shown in the below code snippet.
The below code snippet sets data in the controller. 在MVC中,最重要的部分之一是将数据从控制器传输到视图。Microsoft MVC引入了一个名为“ViewData”的新变量,它将帮助我们在控制器和视图之间传输数据,如下面的代码片段所示。
以下代码段设置控制器中的数据。
Hide Copy Code
ViewData["CurrentTime"] = DateTime.Now.ToString();
The below code snippet displays data on the view.
J2ee Struts framework uses the HTTP request object to pass data from controller to the view. The below code snippet sets a simple date value to the request object using the ‘setAttribute’ function. In ASP.NET MVC we cannot change the request object. J2ee Struts框架使用HTTP请求对象将数据从控制器传递到视图。下面的代码片段使用'setAttribute'函数为请求对象设置一个简单的日期值。在ASP.NET MVC中,我们无法更改请求对象。
request.setAttribute(“CurrentTime”,new Date());
Later we can display the data using ‘getAttribute’.
Conclusion: - First thing both the technologies have the facility of passing data, but somehow j2ee Struts framework thought process looks more convincing. At the end of the day view gets a HTTP request, so it’s more logical to pass data using the request objects rather than creating a new variable like view data for the same.
Many ASP.NET MVC fans (which includes me) can also argue logically that the request object is created by using the data sent by the end user i.e. from the browser. This data should not be allowed to be changed in between by the web application code. The request object should only be allowed to be modified by the end user using POST and GET.
For this I will give equal point to both of them for now. 结论: -首先,这两种技术都具有传递数据的功能,但不知何故,j2ee Struts框架思想过程看起来更具说服力。在可视工作结束后,视图会获取HTTP请求,因此使用请求对象传递数据更合乎逻辑,而不是像创建视图数据一样创建新变量。
许多ASP.NET MVC粉丝(包括我)也可以逻辑地论证请求对象是使用最终用户(即浏览器)发送的数据创建的。不应允许Web应用程序代码之间更改此数据。只允许最终用户使用POST和GET修改请求对象。
为此,我现在将给予他们两个同等的观点。
In ASP.NET MVC the template creates a readymade folder structure (they are termed as areas) which gives a developer a clear picture of where to store the model, views and controllers as shown in the below figure. 在ASP.NET MVC中,模板创建了一个现成的文件夹结构(它们被称为区域),使开发人员可以清楚地了解模型,视图和控制器的存储位置,如下图所示。
In j2EE struts framework we do not have the clear cut vocabulary for folders as we have in ASP.NET MVC. In J2EE framework the controller and model lies in Java resources folder, while the views are saved in web content folder as show in the below diagram. 在j2EE struts框架中,我们没有像ASP.NET MVC那样清晰的文件夹词汇表。在J2EE框架中,控制器和模型位于Java资源文件夹中,而视图保存在Web内容文件夹中,如下图所示。
Said and done you can always manually rename and create different folder structure to have the same logical representation as we have in ASP.NET MVC , only that it’s not automated.
As per our knowledge the above logical structure is not possible currently by using any J2EE struts plug-in either.
Conclusion: - ASP.NET MVC has a slight advantage in terms of better project management due to readymade logical folder structure, while in J2ee framework we need to create them manually. 说完了,你可以随时手动重命名和创建不同的文件夹结构,使其具有与ASP.NET MVC中相同的逻辑表示,只是它不是自动化的。
根据我们的知识,上面的逻辑结构目前也不可能使用任何J2EE struts插件。
结论: -由于现成的逻辑文件夹结构,ASP.NET MVC在更好的项目管理方面略有优势,而在J2ee框架中我们需要手动创建它们。
In ASP.NET MVC you have a nice option where you can create strongly typed view. In other words when you add a view you can select the model with which this view will connect. 在ASP.NET MVC中,您有一个很好的选项,您可以在其中创建强类型视图。换句话说,添加视图时,可以选择此视图将连接的模型。
Later when you go in the view and type model keyword you can get the properties of the object in a strongly typed manner. 稍后当您进入视图并键入model关键字时,您可以以强类型方式获取对象的属性。
In java we do not have the concept of strongly typed view. If you want you can set the object in request.getAttribute and then to a type cast to get the object intellisense. 在java中,我们没有强类型视图的概念。如果需要,可以在request.getAttribute中设置对象,然后设置类型转换以获取对象intellisense。
Conclusion: - This feature can look very exciting for ASP.NET community but it was bit amusing for my J2ee friends (I think they are right in lot of sense also). The whole purpose of strong typed views in ASP.NET MVC is for better intellisense which we can be achieved by type casting data from view data object as shown in the below figure. 结论: -这个功能对于ASP.NET社区来说看起来非常令人兴奋,但对于我的J2ee朋友来说这有点有趣(我认为它们在很多方面也是正确的)。ASP.NET MVC中强类型视图的全部目的是为了更好的智能感知,我们可以通过从视图数据对象中类型转换数据来实现,如下图所示。
The biggest problem here is that developers can start thinking that the model is tied up the view. We understand the intention is not that, but the dialog box just makes a visual intention of doing the same. The end goal of MVC is to separate the model from the view.
So concluding it’s a good feature to get maximum from less code but just that it can be confusing for junior developers who are working on MVC. For a small automation I hope we do not end with a logical confusion about MVC fundamental.
Equal points again to both. I am not giving an extra point to ASP.NET MVC as view thing is more confusing and can be achieved by typecasting. 这里最大的问题是开发人员可以开始认为模型与视图有关。我们理解的意图并非如此,但对话框只是出于同样的目的。MVC的最终目标是将模型与视图分开。
因此总结它是一个很好的功能,可以从更少的代码中获得最大化,但只是对于正在开发MVC的初级开发人员来说这可能会让人感到困惑。对于小型自动化,我希望我们不要以对MVC基础的逻辑混淆而告终。
两者相等。我没有给ASP.NET MVC额外的一点,因为视图事物更加混乱,可以通过类型转换来实现。
A good MVC framework will always provide helper classes to create HTML code for views.
In ASP.NET MVC we have the helper classes. For instance to create a simple HTML form in ASP.NET MVC you can use the HTML helper class as shown in the below code. 一个好的MVC框架将始终提供帮助程序类来为视图创建HTML代码。
在ASP.NET MVC中,我们有助手类。例如,要在ASP.NET MVC中创建一个简单的HTML表单,您可以使用HTML帮助程序类,如下面的代码所示。
In j2ee struts framework we have tag libraries which help us to generate the HTML code as it is done by using ASP.NET MVC html helper classes. 在j2ee struts框架中,我们有标记库,它帮助我们生成HTML代码,类似通过使用ASP.NET MVC html帮助程序类完成的效果。
Conclusion: - Both the frameworks have HTML helper classes. Let’s not get in to which library is better or else we will lose focus on our main comparison. So even points to both the framework on this discussion. 结论: -两个框架都有HTML帮助程序类。让我们不了解哪个库更好,否则我们将失去对主要比较的关注。所以甚至指出了这个讨论的框架。
MVC is all about actions and these actions are mapped to URL. As a developer you would love to see your MVC framework have the capability of customizing and configuring the MVC URL with action mapping. Below is a simple table which explains why developers would expect customization and configuration for MVC URL’s. MVC完全与动作有关,这些动作都映射到URL。作为开发人员,您希望看到您的MVC框架能够使用操作映射自定义和配置MVC URL。下面是一个简单的表,解释了为什么开发人员期望MVC URL的定制和配置。
Incoming URL |
Controller |
Action |
Description |
http://www.questpond.com/LocateProduct |
SearchProductController |
Search |
This action will help us to get all products. |
http://www.questpond.com/LocateProduct/100 |
SearchProductController |
Search(1001) |
This action will help us to get all products with code 1001. |
http://www.questpond.com/Create |
MaintainProductController |
Create |
This action will help us to create a new product. |
http://www.questpond.com/Modify/1001 |
MaintainProductController |
Modify(1001 |
This action will help us to modify product with product code 1001. |
传入的URL |
调节器 |
行动 |
描述 |
http://www.questpond.com/LocateProduct |
SearchProductController |
搜索 |
此操作将帮助我们获取所有产品。 |
http://www.questpond.com/LocateProduct/100 |
SearchProductController |
搜索(1001) |
此操作将帮助我们获取代码为1001的所有产品。 |
http://www.questpond.com/Create |
MaintainProductController |
创建 |
此操作将帮助我们创建新产品。 |
http://www.questpond.com/Modify/1001 |
MaintainProductController |
修改(1001 |
此操作将帮助我们使用产品代码1001修改产品。 |
In ASP.NET MVC this is achieved by using the inbuilt routing mechanism. In order to configure routes you can go to the global.asx.cs code and use the routes collection to map the URL structure with the controllers and actions. 在ASP.NET MVC中,这是通过使用内置路由机制实现的。为了配置路由,您可以转到global.asx.cs代码并使用routes集合将URL结构映射到控制器和操作。
routes.MapRoute(
"HelloWorld", // Route name
"Pages/RegisterAction/{1}", // URL with parameters
new { controller = "Register", action = "RegisterAction", id = UrlParameter.Optional }); // Parameter defaults
In order to configure MVC URL in J2ee struts framework we can use the Struts XML file to the same. The below mapping is more clean than the routing collection of ASP.NET MVC. In J2ee framework we can see the mappings more better as they are mapped directly to page names. 为了在J2ee struts框架中配置MVC URL,我们可以使用Struts XML文件。下面的映射比ASP.NET MVC的路由集合更干净。在J2ee框架中,我们可以更好地看到映射,因为它们直接映射到页面名称。
Conclusion: - J2ee Struts framework definitely wins in terms of MVC URL configuration and mapping to the controller as its defined using XML file. This thing can be really improved in ASP.NET MVC framework. To change the mapping compiling code is more of a burden. 结论: - J2ee Struts框架肯定在MVC URL配置和映射到控制器方面胜出,因为它使用XML文件定义。在ASP.NET MVC框架中可以真正改进这一点。更改映射编译代码更是一种负担。
In ASP.NET MVC we have the advantage of doing URL validation using regex (regular expression) before the action hits the controller. For instance below is a simple validation where before the view customer action is called we can check that the input to the action is only numeric value with 2 digits. 在ASP.NET MVC中,我们的优势是在操作到达控制器之前使用正则表达式(正则表达式)进行URL验证。例如,下面是一个简单的验证,在调用视图客户操作之前,我们可以检查操作的输入是否只是带有2位数的数值。
routes.MapRoute(
"View", // Route name
"View/ViewCustomer/{id}", // URL with parameters
new { controller = "Customer", action = "DisplayCustomer", id = 0 }, new { id = @"\d{1,2}" }
);
Currently J2ee struts framework does not support the same, said and done you can always still do validation after hitting the controller using some custom logic.
Conclusion: - This is definitely a plus point for ASP.NET MVC because MVC URL’s can be invoked from any medium, via browser, via URLs etc. So we would like to ensure that appropriate validation is checked much before it hits the controller. 目前J2ee struts框架不支持相同,可选的方法是你可以在使用一些自定义逻辑,进而在命中控制器后进行验证。
结论: -这绝对是ASP.NET MVC的一个优点,因为MVC URL可以从任何介质,浏览器,URL等调用。所以我们希望确保在它到达控制器之前检查适当的验证。
MVC URL’s are mapped to action and they can be invoked directly which also means that they are subjected to cross site attacks, sql injection etc. ASP.NET MVC framework has provided security attributes by which we can protect our URL from such attacks.
For instance to prevent forgery attacks and cross site scripting attacks we can use the ‘HtmLAntiForgeryToken()’ as shown in the below code snippet. MVC URL被映射到action并且它们可以被直接调用,这也意味着它们受到跨站点攻击和sql注入等。ASP.NET MVC框架提供了安全属性,通过它我们可以保护我们的URL免受此类攻击。
例如,为了防止伪造攻击和跨站点脚本攻击,我们可以使用'HtmLAntiForgeryToken()',如下面的代码片段所示。
In the actions later you can then check if there was any violation. 在稍后的操作中,您可以检查是否存在任何违规。
[HttpPost] [ValidateAntiForgeryToken]
public ActionResult Delete(int customerid)
{
}
You can also mark the controller by using validate input attribute to avoid CSS. 您还可以使用validate input属性标记控制器以避免使用CSS。
[ValidateInput(true)]
public class OurController : Controller
Critical functions on which you do not want actions to be invoked you can mark the methods as ‘nonaction’ as shown in the below code snippet. 您不希望调用操作的关键函数您可以将方法标记为'nonaction',如下面的代码片段所示。
[NonAction]
public void DoPasswordChange(string username, string newpassword)
{
/* Rest of code unchanged */
}
In J2ee Struts framework currently we do not have any inherent security function to achieve the same. Some customized code.
Conclusion: - This is the biggest win for ASP.NET MVC framework security. Hope that J2ee struts framework in coming times has such kind of inherent security feature which will be a great extra added advantage for the framework. 在J2ee Struts框架中目前我们没有任何固有的安全功能来实现相同的功能。一些自定义代码。
结论: -这是ASP.NET MVC框架安全性的最大胜利。希望J2ee struts框架在未来的时代具有这种固有的安全特性,这将是框架的一个额外的附加优势。
Below is the final conclusion. ASP.NET MVC framework out performs J2ee in 4 things while J2ee has the flexible XML URL customization which is currently not available in ASP.NET MVC. For all the other points both of them remain on the same page.
I have tried my level best to put forward both the sides and while doing so I never had in my mind that I am an ASP.NET Microsoft MVP and I should hard sell ASP.NET MVC framework. I have made by best effort to make a true comparison and see which one of them is the best. I do understand how every developer loves his technology, by any chance I have hurted….BIG SORRY. 以下是最终结论。ASP.NET MVC框架在4个方面优于J2ee,而J2ee具有灵活的XML URL自定义,这在ASP.NET MVC中目前不可用。对于所有其他点,它们都保留在同一层次上。
我已经尝试了我最好的水平提出双方,而这样做我从来没有想到我是一个ASP.NET Microsoft MVP,我应该努力销售ASP.NET MVC框架。我努力做出真正的比较,看看哪一个是最好的。我确实理解每个开发者都喜欢他的技术,我有任何机会伤害......。抱歉。