As a result of working with Java and C # Web development, the typical use of the framework is spring MVC3 and MVC3
The following individuals use the code snippet to compare the differences between the two frames. The following: s represents SPRINGMVC3 C on behalf of C # MVC3
1, configuration file:
C:
Global.asax "Routing Configuration"
Web.config "Web configuration such as: Web References, etc."
Controller "must Inherit controller"
S
Web.xml, Servletname-serlvet.xml, contextloaderlistener.xml "Basic configuration of the Web and MVC"
Controller "3.0 is generally used in the way of annotation @Controller"
2, Controller mapping
C:
Routed by:
Routes. Maproute (
"Default",//Route name Route name
"{action}/{id}",//URL with parameters can match such as: user/login/10086
New { Controller = "Home", action = "Index", id = urlparameter.optional}//default configuration
);
Routes. Maproute ("Demo", "Demo/{action}", new {controller = "Demo", action = "Index"});
S
By way of annotation:
@Controller
@RequestMapping (value= "/user") public
class Demo_2_controller {
/**
* This action route is http:/ /localhost:8080/springmvcdemo_2/user/login/{username}
* @param username
* @return * *
Requestmapping (value= "/login/{username}")
//@RequestMapping (value= "/login/{userid}/create"): This is also possible, The requested URL can be "/user/login/{username}/create". Public
Modelandview Login (@PathVariable String username)
{
Modelandview Mav = new Modelandview ("user/loginsuccess");
Mav.addobject ("username", username);
return Mav
}
}
Spring MVC is richer and more free
3. Common statements and annotations
Support Post or Get method
C: The default is to support get and post
<span style= "White-space:pre" > </span>httppost]
[httpget] public
actionresult PayType ()
{return
Partialview ("PayType");
}
S: The default is to support get and post
@RequestMapping (value = "/jsondata", method = {requestmethod.post,requestmethod.get},headers= "content-type= Application/json ")
Facing plane
C: Inherits controller, needs to rewrite its onactionexecuting onactionexecuted
S: There are two ways
① is a way of filtering filter filter:
<bean id= "Logincheck" class= "Vancl.com.filter.LoginInitInterceptor" ></bean>
<!-- Spring3.1 started with annotations handlermapping-->
<bean class= " Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping ">
<property name=" Interceptors ">
<list>
<ref bean=" Logincheck "/>
</list>
</property>
Where Logincheck is a custom filter, you need to inherit Handlerinterceptoradapter, overriding its three methods:
Package vancl.com.filter;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Org.springframework.web.servlet.ModelAndView;
Import Org.springframework.web.servlet.handler.HandlerInterceptorAdapter; /** * Login Detection * * @author yangfenge * */public class Logincheckintercepter extends Handlerinterceptoradapter {@Ove Rride public void Aftercompletion (HttpServletRequest request, httpservletresponse response, Object handler, Exception
Ex) throws Exception {//TODO can do some session and Cookies update request.getsession (). setattribute ("username", "Upxiaofeng");
Request.getsession (). setattribute ("Password", "Upxiaofeng");
Request.getsession (). setattribute ("userid", "10086"); @Override public void Posthandle (HttpServletRequest request, httpservletresponse response, Object handler, Mode Landview Modelandview) throws Exception {//TODO auto-generated Method stub super.posthandle (request, Response, Handl Er, Modelandview); @Override public boolean prehandle (HttpServletRequest request, httpservletresponse response, Object handler) throw
s Exception {//TODO auto-generated Method Stub Boolean flag = FALSE;
Gets a user session that determines whether a user has a string userid = (string) request.getsession (). getattribute ("userid");
String username = (string) request.getsession (). getattribute ("username");
String password = (string) request.getsession (). getattribute ("password"); There is user-determined level login if (username!= null && username!= "" && password!= null && password!= "")
{if (userid!= null && userid!= "") {//TODO authenticate user through flag=true;
}else {//TODO User level login, jump to login interface response.sendredirect ("Login url" +request.getrequesturi ());
} else {//TODO user not logged in, jump to login interface response.sendredirect ("Login url" +request.getrequesturi ());
}
;
return flag;
}
}②spring Pre-Reset
Configure XML
<aop:aspectj-autoproxy/>
<!--enable Log class--> <bean id= "MyLog" class= "Controller.log.MyLog"
> </bean>
<!--AOP configuration proxy-target-class needs to be set to true-->
<aop:config proxy-target-class= " True ' >
<!--expression setting into one or a set of methods-->
<aop:pointcut id= "log" expression= "Execution" (* vancl.*.* (..))" />
<!--aspect Set slice--> <aop:aspect id= "MyLog" ref= "MyLog"
>
<!--before method before execution notification, Method: Notification Methods-->
<aop:before pointcut-ref= "Log" method= "before"/>
<!--after the execution of the method, Method: Notification Methods-->
<aop:after pointcut-ref= "log" method= "after"/>
</aop:aspect>
</ Aop:config>
MyLog Code:
Package controller.log;
Import Org.aspectj.lang.JoinPoint;
public class MyLog {
//write method in class, method name poem can be arbitrary. Here I use standard before and after to express
//Here the Joinpoint class can get, action all the related configuration information and request and other built-in objects. Public
Void before (Joinpoint joinpoint) {
//This method returns an array that includes the request and Actioncofig class objects
object[] Object = Joinpoint.getargs ();
for (Object o:object) {
System.out.println (o);
}
System.out.println (Joinpoint.getclass (). GetMethods ());
System.out.println (Joinpoint.gettarget ());
SYSTEM.OUT.PRINTLN ("Call this method before the blocked method is called, output this statement");
}
public void after (Joinpoint joinpoint) {
System.out.println (called this method after the blocked method call, outputting this statement);
}
It enters mylog when executing all the methods of all the classes in the VANCL package.
Here is just a summary of the more important and commonly used similarities before the two frameworks, where the SPRINGMVC action returns content that contains the view Json string, and so on, and the same return types are in C # MVC.
In general, they are two more useful foreground frames. Personal feel springmvc more flexible, configuration slightly troublesome, C#MVC package thoroughly, use becomes convenient.