struts2的基本配置以及jsp的路径问题

 

1、  在web.xml文件中配置以下内容:
<filter>
        <filter-name>struts2</filter-name>   
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
       <filter-name>struts2</filter-name>
       <url-pattern>/*</url-pattern>

 </filter-mapping>

2、  struts.xml文件的配置
<!--将值设置为true,表明正在处于开发模式,当有修改的时候tomcat会立即有所反应,无需重启 -->

<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
        <action name="hello">
            <result>/hello.jsp</result>
        </action>

</package>

3、  在java文件中选中类名,点击F1,可以查看相应的帮助文档,前提是配置好jar包的javadoc文档。选中jar包,右键Properties,javadoc Location,找到javadoc所在的文档,添加即可。 查看的时候,选中类名,点击F1即可。类似,可以操作源码。

4、  命名空间:namespace决定了action的访问路径,默认为"",可以接收所有路径的action
namespace可以写为/,或者/xxx,或者/xxx/yyy,对应的action访问路径为/index.action, /xxx/index.action,或者/xxx/yyy/index.action.
namespace最好也用模块来进行命名

5、  Action:可以决定具体视图的返回,具体的手段是根据返回的字符串找到对应的配置项,来决定视图的内容。具体Action的实现可以是一个普通的java类,里面有public String execute方法即可,或者实现com.opensymphony.xwork2.ActionSupport接口,不过最常用的是从ActionSupport继承,好处在于可以直接使用Struts2封装好的方法

6、  Path:struts的路径问题是根据action的路径来确定,所以尽量不要使用相对路径。虽然可以使用redirect方式解决,但是redirect方式并非必要。解决办法非常简单,统一使用绝对路径。(在jsp中用request.getContextRoot方式来拿到webapp的路径) ,或者使用myeclipse经常用的,指定basePath

例子程序中的测试页面Path.jsp内容如下:
<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"pageEncoding="GB18030"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%
     String path = request.getContextPath();
     String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";
     System.out.println(path);//"/Struts2_0400_Path"获得的是项目的根路径
     System.out.println(request.getScheme());//“http”,获得的是请求的名字
     System.out.println(request.getServerName());//“127.0.0.1”获得的是服务器的名字
     System.out.println(request.getServerPort());//“8080”获得的是服务器的端口号
     System.out.println(basePath);//“http://127.0.0.1:8080/Struts2_0400_Path/”,组合起来获得的是整个项目的绝对根路径
     System.out.println();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
     <head>
         <base href="<%=basePath%>" /><!-- 当前所有页面中的链接,前面默认会帮忙加上basePath -->
         <meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
         <title>Insert title here</title>
     </head>
     <body>
         <a href="<%=basePath%>index1.jsp">index1.jsp</a>
         <!-- 跳转到正常测index1.jsp页面,因为使用的是绝对路径 -->
         <a href="/index.jsp">index.jsp</a>
         <!-- 跳转到tomcat的欢迎页面,因为“/”代表整个站点的根路径,不是WebApplication的根路径-->
         <a href="index2.jsp">index2.jsp</a>
         <!-- 出现错误,他寻找的是项目路径下,http://127.0.0.1:8080/Struts2_0400_Path/path/index2.jsp这个路径的jsp --><br />
     </body>
</html>
Struts.xml文件内容:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
    <constant name="struts.devMode" value="true" />
    <package name="path" extends="struts-default" namespace="/path">
        <action name="path" class="com.bjsxt.struts2.path.action.PathAction">
            <result name="path">/path.jsp</result>
        </action>
    </package>
</struts>

Index.jsp内容
<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
     pageEncoding="GB18030"%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
     <head>
         <meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
         <title>Insert title here</title>
     </head>
     <body>
         <a href="path/path.action">路径问题说明</a>
     </body>
</html>

解释:
当进入系统的index界面之后,点击链接“路径问题说明”,首先会去struts.xml文件中查找命名空间path下面的名为path的action,由于该action方法返回success,进入path.jsp。在该页面中有三个链接:
1.index1.jsp:其使用的是绝对路径,跳转到Webroot下面的index.jsp
2.index.jsp:它会跳转到tomcat的欢迎页面,因为“/”代表整个站点的根路径,不是WebApplication的根路径
3.index2.jsp:会出现错误,它寻找的是项目路径http://127.0.0.1:8080/Struts2_0400_Path/path/index2.jsp这个路径的jsp,但是实际上的index2.jsp是位于Webroot下面的。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值