Struts入门 相关配置及程序.docx

上传人:b****3 文档编号:6238036 上传时间:2023-05-09 格式:DOCX 页数:14 大小:1.02MB
下载 相关 举报
Struts入门 相关配置及程序.docx_第1页
第1页 / 共14页
Struts入门 相关配置及程序.docx_第2页
第2页 / 共14页
Struts入门 相关配置及程序.docx_第3页
第3页 / 共14页
Struts入门 相关配置及程序.docx_第4页
第4页 / 共14页
Struts入门 相关配置及程序.docx_第5页
第5页 / 共14页
Struts入门 相关配置及程序.docx_第6页
第6页 / 共14页
Struts入门 相关配置及程序.docx_第7页
第7页 / 共14页
Struts入门 相关配置及程序.docx_第8页
第8页 / 共14页
Struts入门 相关配置及程序.docx_第9页
第9页 / 共14页
Struts入门 相关配置及程序.docx_第10页
第10页 / 共14页
Struts入门 相关配置及程序.docx_第11页
第11页 / 共14页
Struts入门 相关配置及程序.docx_第12页
第12页 / 共14页
Struts入门 相关配置及程序.docx_第13页
第13页 / 共14页
Struts入门 相关配置及程序.docx_第14页
第14页 / 共14页
亲,该文档总共14页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

Struts入门 相关配置及程序.docx

《Struts入门 相关配置及程序.docx》由会员分享,可在线阅读,更多相关《Struts入门 相关配置及程序.docx(14页珍藏版)》请在冰点文库上搜索。

Struts入门 相关配置及程序.docx

Struts入门相关配置及程序

修改字体大小:

wiondow--preferences--general--appearance--colorsandfonts--java--javaeditortextfont

然后点change,在弹出的框里选字号,然后确定就可以了~~

预备知识:

环境搭建:

1.导入jar包

2.配置文件struts-config.xml和web.xml介绍

Østruts-config.xml:

灵活配置

Øweb.xml:

✓注册Struts核心控制器——ActionServlet

✓struts-config.xml文件的位置

配置servlet

action

org.apache.struts.action.ActionServlet

config

/WEB-INF/struts-config.xml

跟初始化相关的。

指向struts-config.xml

思考:

struts-config.xml可以改名吗?

0在服务一启动就实例化

action

*.do

一、一个简单的Struts应用

1.开发Form

●建立一个类,继承ActionForm

●在配置文件中注册该Form

name="loginForm"type="my.example.LoginForm">

2.开发Action

●建立一个类,继承Action

覆盖execute方法

publicActionForwardexecute(ActionMappingmapping,ActionFormform,

HttpServletRequestrequest,HttpServletResponseresponse)

throwsException{

LoginFormloginForm=(LoginForm)form;

if(loginForm.getUsername().equals("mary"))

returnmapping.findForward("loginSuccess");

else

returnmapping.findForward("loginFailure");

}

●在配置文件中注册该Action

3.JSP文件

Login.jsp

/login.do"method="post">

username:


password:


 

request.getContextPath()获取系统路径

 

LoginFailure.jsp

execute方法:

第一步:

强制转型为ActionForm子类

第二步:

业务逻辑处理

第三步:

跳转

几次读取配置文件?

如果配置文件里没有这个Action?

试一试会怎样?

将action-mapping内容删掉,看看错误。

将action标签中的loginform去掉,

报错误:

空指针

带的form为空

将action标签中的loginForm写错

报错误:

空指针

带错了。

改写execute方法:

publicActionForwardexecute(ActionMappingmapping,ActionFormform,

HttpServletRequestrequest,HttpServletResponseresponse)

throwsException{

LoginFormloginForm=(LoginForm)form;

Stringforwardkeywords="";

if(loginForm.getUsername().equals("mary"))

forwardkeywords="loginSuccess";

else

forwardkeywords="loginFailure";

returnmapping.findForward(forwardkeywords);

}

将action标签中的loginForm去掉,将execute中的业务逻辑处理去掉,

没有报异常,因此可以

但是关键字为空,在配置文件中找不到,因此什么也不显示

将jsp文件中name改为username1

报nullpointerexception

测试:

在execute方法中在第二句写上System.out.println(loginForm);

思考:

1.谁来填充Form?

什么时候填充?

根据什么填充?

2.ActionServlet怎样把请求派发给Action?

3.Action运行完后怎样跳转?

使用Struts增加学生

1.配置Struts环境

2.addStudentForm(extendsActionForm):

sId,sname,major,birth,score

3.addStudentAction(extendsAction)

覆盖execute方法

IStudentDAO,studentDAO

addStudent(addSutdentFormstudentForm)

4.jsp页面:

addStudent.jspaddStudentSuccess.jspaddStudentFailure.jsp

5.

ActionForm描述:

第一步:

第二步:

修改配置文件

第三步:

创建AddStudentAction类并修改struts配置文件。

失败返回初始页面

第四步:

创建接口DAO

第五步:

写execute方法

Execute方法

第六步:

写DAO类

此时可以先测试通不通?

此处不全

struts工作原理

1.初始化struts框架总控制器,需要读取struts-config.xml

2.等待客户发出http请求,.do的请求就可以

3.填充FormBean

4.将请求转换到具体Action处理

5.调用后台业务功能类完成商业或业务逻辑。

不论如何调用,最终还是回到Action

第五步跟Struts没什么关系

6.回到ActionServlet,返回目标响应对象

7.转换http请求到目标响应对象,即找到相应的jsp文件

8.http响应,显示在客户端

总结这八步:

什么时候读取配置文件struts-config.xml?

启动的时候

请求先进入服务器,.do的请求才进struts控制器。

填充form就是将一个对象的成员变量放置对应的值。

首先要构建一个对象——实例化,

实例化后就进行填充数据,最后还保存。

目的是将form传给action用。

填充数据时数据来自用户提交的表单数据。

根据什么去找action?

struts-config.xml的action标签

注意是将结果展现给用户

JSP不是在客户端运行的

配置文件的作用:

把这些常用控件组合在一起

form-bean是否类似于变量的定义?

如果配置文件中action标签的path写为”login.do”则报错

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2