struts1了解和WEB-INF受保护目录下jsp的访问

本文介绍了Struts1的配置步骤,包括所需库、配置文件和web.xml设置,并讨论了如何在WEB-INF保护目录下通过不同方式实现JSP之间的互相访问,包括使用jsp:forward、ActionForward、RequestDispatcher以及处理资源路径的问题。

struts1了解

 

1.建好工程后,WEB-INF/lib加入如下包:commons-beanutils.jar,commons-collections-2.1.1.jar,commons-digester.jar,commons-fileupload.jar,commons-logging.jar,commons-validator.jar,jakarta-oro.jar,struts.jar
2.WEB-INF下加下如下文件:struts-bean.tld,struts-html.tld,struts-logic.tld,struts-nested.tld,struts-tiles.tld,struts-config.xml(此文件很关键:主要配置此文件)--tld文件在struts.jar/META-INF/tlds包内有
3.修改web.xml:---<servlet>和<servlet-mapping>
<servlet-name>action</servlet-name>
  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<param-name>config</param-name>
    <param-value>/WEB-INF/struts-config.xml</param-value>
<servlet-name>action</servlet-name>
   <url-pattern>*.do</url-pattern>

 

4.写自已的MyAction要继承Action
5.修改struts-config.xml配置文件--普通XML文件.在第二行增加如下描述:
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">.
6.然后增加<struts-config>标签---根据MyAction类配置struts-config.xml
7.最好在IE中运行,可能在ECLIPSE自带的浏览器中运行提示404错误.
8.第一次请求时MyAction会实例.后面再次请求就不会实例(即服务不重启情况下就实例一次)

9.jsp页面使用STRUTS1标签--如:<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic" %>

 

10.STRUTS1资源国际化(在SRC目录下):默认资源文件:MessageResources.properties,中文:MessageResources_zh_CN.properties,英文:MessageResources_en_US.properties.界面<bean:message key="xxx"/>来显示.
11.validator-rules.xml需要放到WEB-INF目录下,自定义validation.xml文件来校验JSP表单

12.出现错误:javax.servlet.jsp.JspException: Cannot find ActionMappings or ActionFormBeans collection
在web.xml的SERVLET中加上<load-on-startup>0</load-on-startup>
13.出现错误:Document root element "struts-config", must match DOCTYPE root "struts".
struts-config.xml文件中的<!DOCTYPE struts-config--这是与标签不匹配

 

14.logic:iterator标签的嵌套举例
<logic:iterate id="address" name="user" property="addressList" length="3" offset="1">                          <bean:write name="address"/></logic:iterate>--addressList是user对象属性(是集合),address是集合的元素.
jspFOREACH标签:<c:forEach var="message" items="${user.messages}">--user.messages是集合,message是集合元素

15.<html:errors/>界面显示错误,注意要在ACTION中saveErrors(request, messages);
16.struts-config.xml配置文件中可以配置拦截异常(全局和ACTION局部).但好定义一个ACTION的基类(实现异常处理),其它ACTION类都要继承它.出错页面中可以通过pageContext对象得到请求路径/状态码/异常等

 

WEB-INF受保护目录下jsp之间的互相访问

我们说WEB-INF目录下有 *.xml classes lib 等目录和文件,它们一般都是不让直接访问的。

 

说明这个目录是安全的,我们回想为什么不把jsp、html等页面文件放进去呢?

 

这样会不会安全一些呢?大家猜的不错,这样是安全了(使用过滤器也可以实现该功能),

 

有一个路径问题需要解决,使用页面入口问题,如果页面文件放在WEB-INF目录下,用户访问

 

WEB-INF目录下页面文件会报找不到页面,用户该怎么才能访问到网站页面呢?

 

如果liandong.jsp放在WEB-INF/jsp目录下,Web Context-root为:liandong那么

 

1.我们可以在WebRoot下新建index.jsp,其中的代码为:

<jsp:forward page="WEB-INF/jsp/liandong.jsp"></jsp:forward>

 

2.我们可以在struts-config.xml配置代码:

<action path="/test" scope="request"
    type="org.springframework.web.struts.DelegatingActionProxy">
  <forward name="liandong" path="/WEB-INF/jsp/liandong.jsp"></forward>   
   </action>

 

action 中代码为:

@Override
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  // TODO Auto-generated method stub
  // method 2
  return mapping.findForward("liandong");
 }

index.jsp代码为:<a href="test.do">link-->liandong</a>

 

3.我们可以在action 中写代码:

@Override
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  // TODO Auto-generated method stub
  System.out.println("bbb");
  RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/jsp/liandong.jsp");

  rd.forward(request,response);

  return null;
 }

index.jsp代码为:<a href="test.do">link-->liandong</a>

struts-config.xml配置代码:

<action path="/test" scope="request"
    type="org.springframework.web.struts.DelegatingActionProxy">
  <forward name="liandong" path="/WEB-INF/jsp/liandong.jsp"></forward>   
   </action>

4.action 中

@Override
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  // TODO Auto-generated method stub
  System.out.println("bbb");

  return new ActionForward("/WEB-INF/jsp/liandong.jsp");
 }

index.jsp代码为:<a href="test.do">link-->liandong</a>

struts-config.xml配置代码:

<action path="/test" scope="request"
    type="org.springframework.web.struts.DelegatingActionProxy">
  <forward name="liandong" path="/WEB-INF/jsp/liandong.jsp"></forward>   
   </action>

 

还有个问题就是,层叠样式文件、js脚本文件、图片文件的路径

1.页面访问图片

background-image: url('/liandong/images/bg.bmp')

2.页面访问层叠样式文件

<link rel="stylesheet" href="/liandong/css/liandong.css" type="text/css"></link>

3.页面访问js脚本文件

<script type="text/javascript" src="/liandong/js/liandong.js"></script>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值