S MVC face questions summary

1. What is Spring MVC? A brief introduction of your understanding of springMVC?

Spring MVC is a Java-based implementation of the request for driving the MVC design pattern type lightweight Web framework, the web layer functions to decouple the Model, View, Controller separation, the complex web application divided into several logic clear part, to simplify development, reduce error, the fit between the easy to develop group.

 

2, SpringMVC process?

(1) The user sends a request to the front-end controller the DispatcherServlet;
(2) the DispatcherServlet after receiving the request, the call processor HandlerMapping mapper request to obtain the Handle;
(. 3) the processor finds particular mapping processor generation processing according to the request url blocker object and a processor (if it is generated) collectively returned to the DispatcherServlet;
(. 4) to call the DispatcherServlet HandlerAdapter processor adapter;
(. 5) adapted through HandlerAdapter specific call processor (Handler, also called back-end) ;
(. 6) perform complete Handler returns ModelAndView;
(. 7) Handler HandlerAdapter is the execution result is returned to ModelAndView the DispatcherServlet;
(. 8) to the DispatcherServlet ModelAndView pass parser parses ViewResolver view;
return after DETAILED view (. 9) analytical ViewResolver;
(10 ) view of the DispatcherServlet render view (model data coming filled view)
(. 11) in response to the DispatcherServlet user.

 

3, Springmvc advantages:

(1) may support various view technologies, not just the JSP;

(2) integrated with the Spring framework (e.g. IoC container, AOP, etc.);

(3) a clear role assignment: front-end controller (dispatcherServlet), maps the request to the processor (handlerMapping), the adapter processor (HandlerAdapter), view resolver (ViewResolver).

(4) supports a variety of mapping policies of the requested resource.

 

4, Spring MVC's major components?

(1) the DispatcherServlet front controller (not require programmers to develop)

Action: receiving a request, a response result, the equivalent of the transponder, with DispatcherServlet reduces the coupling between the other components.

(2) the HandlerMapping mapper processor (not require programmers to develop)

Role: According to the URL request to find Handler

(3) Processor Adapter HandlerAdapter

Note: To follow the rules HandlerAdapter required to write at the time of writing Handler, so that we can correct adapter HandlerAdapter to perform Handler.

(4) Processor Handler (requires programmers to develop)

(5) ViewResolver view resolver (not require programmers to develop)

Action: parsing view, a view of the logical name resolves to a real view (view)

(6) View View (requires programmers to develop jsp)

View is an interface, the implementation of which support different types of views (jsp, freemarker, pdf, etc.)

 

5, the difference springMVC and struts2 What?

(1) springmvc i.e. inlet is a front controller servlet (DispatchServlet), and a filter inlet struts2 worrying too much (StrutsPrepareAndExecuteFilter).

(2) springmvc method is based on the development of (a method corresponding to a url), request parameters passed to the method parameter, or can be designed as a single Example Example (recommended singleton), Struts2 is class-based development, the parameter is passed through the class the property can only be designed to be more cases.

(3)Struts采用值栈存储请求和响应的数据,通过OGNL存取数据,springmvc通过参数解析器是将request请求内容解析,并给方法形参赋值,将数据和视图封装成ModelAndView对象,最后又将ModelAndView中的模型数据通过reques域传输到页面。Jsp视图解析器默认使用jstl。

 

6、SpringMVC怎么样设定重定向和转发的?

(1)转发:在返回值前面加"forward:",譬如"forward:user.do?name=method4"

(2)重定向:在返回值前面加"redirect:",譬如"redirect:http://www.baidu.com"

 

7、SpringMvc怎么和AJAX相互调用的?

通过Jackson框架就可以把Java里面的对象直接转化成Js可以识别的Json对象。具体步骤如下 :

(1)加入Jackson.jar

(2)在配置文件中配置json的映射

(3)在接受Ajax方法里面可以直接返回Object,List等,但方法前面要加上@ResponseBody注解。

 

8、如何解决POST请求中文乱码问题,GET的又如何处理呢?

(1)解决post请求乱码问题:

在web.xml中配置一个CharacterEncodingFilter过滤器,设置成utf-8;

<filter>

    <filter-name>CharacterEncodingFilter</filter-name>

    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

    <init-param>

        <param-name>encoding</param-name>

        <param-value>utf-8</param-value>

    </init-param>

</filter>

<filter-mapping>

    <filter-name>CharacterEncodingFilter</filter-name>

    <url-pattern>/*</url-pattern>

</filter-mapping>

 

(2)get请求中文参数出现乱码解决方法有两个:

①修改tomcat配置文件添加编码与工程编码一致,如下:

<ConnectorURIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

 

 ②另外一种方法对参数进行重新编码:

String userName = new String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8")

ISO8859-1是tomcat默认编码,需要将tomcat编码后的内容按utf-8编码。

 

9、Spring MVC的异常处理 ?

答:可以将异常抛给Spring框架,由Spring框架来处理;我们只需要配置简单的异常处理器,在异常处理器中添视图页面即可。

 

10、SpringMvc的控制器是不是单例模式,如果是,有什么问题,怎么解决?

答:是单例模式,所以在多线程访问的时候有线程安全问题,不要用同步,会影响性能的,解决方案是在控制器里面不能写字段。

 

11、 SpringMVC常用的注解有哪些?

@RequestMapping:用于处理请求 url 映射的注解,可用于类或方法上。用于类上,则表示类中的所有响应请求的方法都是以该地址作为父路径。

@RequestBody:注解实现接收http请求的json数据,将json转换为java对象。

@ResponseBody:注解实现将conreoller方法返回对象转化为json对象响应给客户。

 

12、SpingMvc中的控制器的注解一般用那个,有没有别的注解可以替代?

答:一般用@Conntroller注解,表示是表现层,不能用别的注解代替。

 

13、如果在拦截请求中,我想拦截get方式提交的方法,怎么配置?

答:可以在@RequestMapping注解里面加上method=RequestMethod.GET。

 

14、怎样在方法里面得到Request,或者Session?

答:直接在方法的形参中声明request,SpringMvc就自动把request对象传入。

 

15、如果想在拦截的方法里面得到从前台传入的参数,怎么得到?

答:直接在形参里面声明这个参数就可以,但必须名字和传过来的参数一样。

 

16、如果前台有很多个参数传入,并且这些参数都是一个对象的,那么怎么样快速得到这个对象?

答:直接在方法中声明这个对象,SpringMvc就自动会把属性赋值到这个对象里面。

 

17、SpringMvc中函数的返回值是什么?

答:返回值可以有很多类型,有String, ModelAndView。ModelAndView类把视图和数据都合并的一起的,但一般用String比较好。

 

18、SpringMvc用什么对象从后台向前台传递数据的?

答:通过ModelMap对象,可以在这个对象里面调用put方法,把对象加到里面,前台就可以通过el表达式拿到。

 

19、怎么样把ModelMap里面的数据放入Session里面?

答:可以在类上面加上@SessionAttributes注解,里面包含的字符串就是要放入session里面的key。

 

20、SpringMvc里面拦截器是怎么写的:

有两种写法,一种是实现HandlerInterceptor接口,另外一种是继承适配器类,接着在接口方法当中,实现处理逻辑;然后在SpringMvc的配置文件中配置拦截器即可:

<!-- 配置SpringMvc的拦截器 -->

<mvc:interceptors>

<!-- 配置一个拦截器的Bean就可以了 默认是对所有请求都拦截 -->

<bean id="myInterceptor" class="com.zwp.action.MyHandlerInterceptor"></bean>

<!-- 只针对部分请求拦截 -->

<mvc:interceptor>

<mvc:mapping path="/modelMap.do" />

<bean class="com.zwp.action.MyHandlerInterceptorAdapter" />

</mvc:interceptor>

</mvc:interceptors>
 

 

21、注解原理:

注解本质是一个继承了Annotation的特殊接口,其具体实现类是Java运行时生成的动态代理类。我们通过反射获取注解时,返回的是Java运行时生成的动态代理对象。通过代理对象调用自定义注解的方法,会最终调用AnnotationInvocationHandler的invoke方法。该方法会从memberValues这个Map中索引出对应的值。而memberValues的来源是Java常量池。

原文参:https://blog.csdn.net/a745233700/article/details/80963758

Guess you like

Origin www.cnblogs.com/lucky1024/p/11120818.html