Java面试题集(六)

以下为框架补充部分:

Struts 2中,Action通过什么方式获得用户从页面输入的数据,又是通过什么方式把其自身的数据传给视图的?

Action从页面获取数据有三种方式:

①通过Action属性接受参数

②通过域模型获取参数

③通过模型驱动获取参数 (ModelDriven<T>

Action将数据存入值栈(Value Stack)中,视图可以通过表达式语言(EL)从值栈中获取数据。

阐述Struts 2中的Action如何编写?Action是否采用了单例?

Struts2Action有三种写法:

POJO

②实现Action接口重写execute()方法

③继承ActionSupport

Action没有像Servlet一样使用单实例多线程的工作方式,很明显,每个Action要接收不同用户的请求参数,这就意味着Action是有状态的,因此在设计上使用了每个请求对应一个Action的处理方式。


Hibernate如何实现分页查询?

通过Hibernate实现分页查询,开发人员只需要提供HQL语句、查询起始行数(setFirstresult()方法)和最大查询行数(setMaxResult()方法),并调用Query接口的list()方法,Hibernate会自动生成分页查询的SQL语句。


web.xml 的作用?

用于配置Web应用的相关信息,如:监听器(listener)、过滤器(filter)、Servlet、相关参数、会话超时时间、安全验证方式、错误页面等。

例如:

①配置Spring上下文加载监听器加载Spring配置文件:

[html] view plain copy
  1. <context-param>      
  2.    <param-name>contextConfigLocation</param-name>      
  3.   <param-value>classpath:applicationContext.xml</param-value>      
  4. </context-param>      
  5. <listener>      
  6.    <listener-class>      
  7.      org.springframework.web.context.ContextLoaderListener      
  8.    </listener-class>      
  9. </listener>    

②配置SpringOpenSessionInView过滤器来解决延迟加载和Hibernate会话关闭的矛盾:

[html] view plain copy
  1. <filter>      
  2.   <filter-name>openSessionInView</filter-name>      
  3.   <filter-class>      
  4.      org.springframework.orm.hibernate3.support.OpenSessionInViewFilter      
  5.   </filter-class>      
  6. </filter>      
  7. <filter-mapping>      
  8.   <filter-name>openSessionInView</filter-name>      
  9.   <url-pattern>/*</url-pattern>      
  10. </filter-mapping>  

③配置会话超时时间为10分钟:

[html] view plain copy
  1. <session-config>      
  2.      <session-timeout>10</session-timeout>      
  3.    </session-config>  

④配置404Exception的错误页面:

[html] view plain copy
  1. <error-page>      
  2.   <error-code>404</error-code>      
  3.   <location>/error.jsp</location>      
  4. </error-page>        
  5. <error-page>      
  6.   <exception-type>java.lang.Exception</exception-type>      
  7.   <location>/error.jsp</location>      
  8. </error-page>   

⑤配置安全认证方式:

[html] view plain copy
  1.   <security-constraint>      
  2.       <web-resource-collection>      
  3.        <web-resource-name>ProtectedArea</web-resource-name>      
  4.         <url-pattern>/admin/*</url-pattern>      
  5.        <http-method>GET</http-method>      
  6.         <http-method>POST</http-method>      
  7.       </web-resource-collection>      
  8.       <auth-constraint>      
  9.         <role-name>admin</role-name>      
  10.       </auth-constraint>      
  11.     </security-constraint>             
  12.     <login-config>      
  13.       <auth-method>BASIC</auth-method>      
  14.     </login-config>      
  15.     <security-role>      
  16.       <role-name>admin</role-name>      
  17. </security-role>  


你的项目中使用过哪些JSTL标签?

项目中主要使用了JSTL的核心标签库,包括<c:if><c:choose><c: when><c: otherwise><c:forEach>等,主要用于构造循环和分支结构以控制显示逻辑。

说明:虽然JSTL标签库提供了coresqlfmt(日期或数字格式化)、xml等标签库,但是实际开发中建议只使用核心标签库(core),而且最好只使用分支和循环标签并辅以表达式语言(EL),这样才能真正做到数据显示和业务逻辑的分离,这才是最佳实践。

如何在Web项目中配置SpringIoC容器?

如果需要在Web项目中使用SpringIoC容器,可以在Web项目配置文件web.xml中做出如下配置:

[html] view plain copy
  1. <context-param>      
  2.     <param-name>contextConfigLocation</param-name>      
  3.     <param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>      
  4. </context-param>      
  5. <listener>      
  6.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>      
  7. </listener>   

如何在Web项目中配置Spring MVC

[html] view plain copy
  1. <servlet>  
  2. <description>spring mvc servlet</description>  
  3. <servlet-name>springMvc</servlet-name>  
  4. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  5. <init-param>  
  6. <description>spring mvc 配置文件</description>  
  7. <param-name>contextConfigLocation</param-name>  
  8. <param-value>classpath:spring-mvc.xml</param-value>  
  9. </init-param>  
  10. <load-on-startup>1</load-on-startup>  
  11. </servlet>  
  12. <servlet-mapping>  
  13. <servlet-name>springMvc</servlet-name>  
  14. <url-pattern>*.do</url-pattern>  
  15. </servlet-mapping>  

如何在Spring IoC容器中配置数据源?

1.DBCP配置:

[html] view plain copy
  1. <bean id="dataSource"      
  2.         class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">      
  3.     <property name="driverClassName" value="${jdbc.driverClassName}"/>      
  4.     <property name="url" value="${jdbc.url}"/>      
  5.     <property name="username" value="${jdbc.username}"/>      
  6.     <property name="password" value="${jdbc.password}"/>      
  7. </bean>          
  8. <context:property-placeholder location="jdbc.properties"/>    

2.C3P0配置:

[html] view plain copy
  1. <bean id="dataSource"      
  2.         class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">      
  3.     <property name="driverClass" value="${jdbc.driverClassName}"/>      
  4.     <property name="jdbcUrl" value="${jdbc.url}"/>      
  5.     <property name="user" value="${jdbc.username}"/>      
  6.     <property name="password" value="${jdbc.password}"/>      
  7. </bean>       
  8. <context:property-placeholder location="jdbc.properties"/>   

3.Druid配置:

[html] view plain copy
  1. <bean name="dataSource"  
  2.       class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
  3. <property name="url" value="${jdbc.url}" />  
  4. <property name="username" value="${jdbc.username}" />  
  5. <property name="password" value="${jdbc.password}" />  
  6. </bean>       
  7. <context:property-placeholder location="jdbc.properties"/> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值