尚学堂马士兵struts2 课堂笔记(一)

06_尚学堂马士兵_Struts2_Struts2_HelloWorld_5

 <constant name="struts.devMode" value="true" />
在默认情况下,刚修改struts.xml后在tomcat中没有立刻刷新要么重新启动tomcat,要么加上上面的常量 dev development


07_尚学堂马士兵_Struts2_Struts2_HelloWorld_6

在eclipse里的struts.xm文件敲<是没有反应的,eclipse没有给出提示,其实eclipse里面也不知道给给出怎么样的提示.

很简单 看struts.xml文件的最上面几行

http://struts.apache.org/dtds/struts-2.0.dtd

这个url就是struts的参考信息,每次去网上拿就比较慢. 打开下载的struts lib文件找到struts2-core-xxx.jar解压缩 在里面就能看到dtd文件

再下来就比较简单了 window preference 在查找里面写catalog 找到xml如下图


09_尚学堂马士兵_Struts2_Struts2_HelloWorld_7_2.

   Struts的运行机制

客户端的请求通过相应的端口交给tomcat,tamcat在通过自己的web.xml把请求交给响应的webapp,webapp通过自己的web.xml里面

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<!-- 让Struts2的核心Filter拦截所有请求 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

找到struts.xmlstruts里面通过命名空间/actionname/result(其中命名空间后面再谈)找到响应的映射地址 返回给客户端.

其实struts干了什么,这么简单的问题为什么要搞这么复杂?

这就叫做简单问题复杂化,你只有在学习的时候能把简单的问题复杂化,当面对复杂工作的时候,你才能把复杂问题简单化.其实设计模式不就是干了个这事吗?

言归正传,struts到底干了什么?

就是把请求与视图分开!


10_尚学堂马士兵_Struts2_Struts2_Namespace_命名空间

Struts里面有package.这东西是干什么用的?

其实它和java里面的package一样.区分类而已,如果有两个action 都叫loginaction要给是前台的一个是后台的.放到要给package里面肯定不行 所以就有两个package 一个front一个back 

ok 就这么简单就 这么方便,甚至不需要动笔 人人都要称呼一声神童啊!

     <package name="main" extends="struts-default" namespace="/">
        <action name="index">
            <result>/Namespace.jsp</result>
        </action>
    </package>

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

在上面的namespace里是因此我们要访问这个actionlocalhost:8800/webname/index.action  (.action可以省略)

如果改成 namespace="/aa/"那么访问的url就是localhost:8800/webname/aa./index.action  (.action可以省略)以此类推

另外也可以给namespace赋为空”” 这个时候其他namespace处理不了的请求都交给它来处理.

另外 项目复制后

这位置不会改变,点项目邮件properties 上面查找webok


11_尚学堂马士兵_Struts2_Struts2_Action

 <package name="front" extends="struts-default" namespace="/">
        <action name="index" class="com.bjsxt.struts2.front.action.IndexAction1">
            <result name="success">/ActionIntroduction.jsp</result>
        </action>
</package>

index这个action指定的class可以有三种写法

一最普通的类

public class IndexAction1 {

public String execute() {
    return "success";
}
}

二实现action接口

import com.opensymphony.xwork2.Action;

public class IndexAction2 implements Action {

@Override
public String execute() {
    return "success";
}

}

三继承actionSupport

public class IndexAction3 extends ActionSupport {

@Override

public String execute() {

    return "success";

}

}

选择那种方式 ?

第三种!

第一种得自己写,第二种只是实现了action接口,actionsupport里面已经给我们写好了一些常用的方法.而且actionsupport本身就实现了action接口

 

再另外

 <action name="index">
            <result>/Namespace.jsp</result>
 </action>

这里面怎么没有 class 

没有制定classs就默认使用actionsupport,actionsupport本身的execute方法返回的就是succes,再看rusult,如果要指定返回success的响应页面,那么success就可以不写.

在上面这种情况下,就是只要你请求index.action我就给你返回Namespace.jsp

 

12_尚学堂马士兵_Struts2_Struts2_Path_路径问题

Struts里面的如下

 <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>


类如下

package com.bjsxt.struts2.path.action;

 
public class PathAction {
public String execute() {
return "path";
}
}


Index.jsp如下(index.jsp也在webroot下 和path.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>


Psth.jsp如下(path.jsp放在webroot目录下)

<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
    <%@taglib uri="/struts-tags" prefix="s" %>
  

<!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>
struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径。<br />
<a href="index.jsp">index.jsp</a>
<br />
虽然可以用redirect方式解决,但redirect方式并非必要。
<br />
解决办法非常简单,统一使用绝对路径。(在jsp中用request.getContextRoot方式来拿到webapp的路径)
<br />
或者使用myeclipse经常用的,指定basePath
</body>
</html>

Index.jspwebapp的默认页面,最开始访问localhost:8080/webname/是就会看到index.jsp此时点击”路径问题说明”就跳转到了path.jsp

path.jsp里面你点击index.jsp跳转到了

Localhost:8080/webname/path/index.jsp 看到问题了吧 系统不用关注文件的真实路径.当你在path.jsp.里面点击index时它的路径就是/path!!

怎么办?使用绝对路径

绝对路径方法一

jsp加上如下java

    <%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

超链接改成

<a href="<%= bathpath %>index.jsp">index.jsp</a>

绝对路径方法二

在头部加上

<base href="<%=basePath%>" />

下面的路径就默认为bathpath

<a href="index.jsp">index.jsp</a> 这样就可以访问了

 

13_尚学堂马士兵_Struts2_Struts2_ActionMethod_DMI_动态方法调用

 
<struts>
    <constant name="struts.devMode" value="true" />
    <package name="user" extends="struts-default" namespace="/kkk">
       
       <action name="userAdd" class="com.bjsxt.struts2.user.action.UserAction" method="add">
            <result>/user_add_success.jsp</result>
        </action>        

        <action name="user" class="com.bjsxt.struts2.user.action.UserAction">
            <result>/user_add_success.jsp</result>
        </action>
 
<span style="font-family:Times New Roman;">        </span></package>

</struts>

  当在浏览器里敲http://localhost:8080/webapps/kkk/userAdd就可以调用这个action,因为后面跟有有个method=”add”所以调用的不是里面的execute()而是add方法.(不鼓励使用)

   第二中就是http://localhost:8080/webapps/kkk!Add(DMI dynamic method invocation) 不过若是你直接就输入上面的url还是不行因为struts2默认不适用dmi所以还得加上

<constant name="struts.enable.DynamicMethodInvocation" value="true"/>

UserAction的类

package com.bjsxt.struts2.user.action;

import com.opensymphony.xwork2.ActionSupport;

 

public class UserAction extends ActionSupport {
public String add() {
     return SUCCESS;
  }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值