`
gg19861207
  • 浏览: 181757 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

struts学习笔记

阅读更多

一:标签使用的声明方式:

<%@ taglib uri=http://struts.apache.org/tags-bean” prefix=”bean” %>

<%@ taglib uri=http://struts.apache.org/tags-html” prefix=”html” %>

二:可以在web.xml文件中手工定义一个uri

web.xml文件中定义一个jsp-config元素

<jsp-config>

<taglib>

<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-html.tld</taglib-location>

</taglib>

</jsp-config>

三:web.xml文件又被称为deployment descriptor

这个文件服务器自动读取其中的配置

四:struts中的转向方式

分为两种情况:

1、使用ActionForward对象来转向

return new ActionForward(“/index.jsp”).

2、在配置文件中配置forward节点

<forward name=”index” path=”/index.jsp” redirect=””></forward>

Return mapping.findForward(“index”);

如果设置redirect=true,则表示使用的是response.sendRedirect()方式,则在跳转后的页面通过request.getAttribute()是得不到跳转前的页面设置的属性值的。反之,如果redirect=false,则表示采用的是

request.getRequestDispatcher(“index.jsp”).forward(request,response)的方式,该方式是可以在页面之间传递属性值。

一:struts标签库

常用的标签库有三类:

html标记:展示一些html元素,比如

html:form,html:submit,html:text,html:password

bean 标记:用来输出一些bean内容,比如bean:write,bean:define

logic标记:用来实现逻辑操作,比如logic:equals,logic:iterator

二:使用标签的两种模式:

1、使用tld文件中默认uri配置

2、web.xml文件中设置tag-lib节点

三:配置和引入用户标签库

第一种方式:Web.xml文件配置

<jsp-config>

<taglib>

<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-html.tld</taglib-location>

</taglib>

</jsp-config>

第二种方式:页面引用

<%@ taglib uri=/WEB-INF/struts-html.tld” prefix=”bean” %>

红色部分必须保证一致

备注:表示资源定位的一般以斜杠开头。

四:常用标签示例

<html:select property=”sex”>

<html:option value=”male”></html:option>

<html:option value=”female”></html:option>

</html:select>

在对应的formbean中对应的属性值用String类型。

1<html:radio>标签

<html:radio property=”sex” value=”male”></html:radio>

<html:radio property=”sex” value=”female”><html:radio>

<html:radio property=”sex” value=”unknown”>未知<html:radio>

在对应的formbean中对应的属性值用String类型。

2<html:checkbox>标签

<html:checkbox property=”fav” value=”reading”>读书</html:checkbox>

<html:checkbox property=”fav” value=”news”>新闻</html:checkbox>

<html:checkbox property=”fav” value=”music”>音乐</html:checkbox>

在对应的formbean中对应的属性值用String【】类型。

取值的时候用String[] fav = loginForm.getFavorite()

try{

for(int i=0;i<fav.length;i++){

response.getWriter().println(fav[i]);

}

}

即可。

3<html:image>标签

<html:image alt=”显示图片” src=”sunset.jpg”></html:image>

<html:img alt=”显示图片” src=”sunset.jpg”></html:image>

备注:src=”sunset.jgp”中的sunset.jpg存放在WebRoot文件夹下。

4<html:link>标签

<html:link href=”A.jsp”>链接</html:link>

<html:link href=”login.do?username=zhangsan”>链接</html:link>

5<html:hidden>标签

<html:hidden property=”flag” value=”delete”/>

6<html:multibox>标签

<html:multibox property=”fav” value=”sport”> </html:multibox>体育

<html:multibox property=”fav” value=”sport”> </html:multibox>财经

<html:multibox property=”fav” value=”sport”> </html:multibox>旅游

7Cookie的使用

<%

Cookie ck = new Cookie(“username”,administrator)

ck.setMaxAge(1000*60*60);

response.addCookie(ck);

Cookie[] cks = request.getCookies();

For(int i=0;i<cks.length;i++){

Cookie ck = cks[i];

Out.println(ck.getName());

}

%>

Cookie标签使用:

<bean:cookie name=”username” id=”a”/>

<bean:write name=”a” property=” name”/>

<bean:write name=”a” property=” value”/>

8<bean:difine>标签

传统方式:

<%

String username = “administrator”;

Out.println(username);

%>

<bean:define id=”myname” value=”administrator”></bean:define>

<bean:write name=”myname”/>

/*request范围内的一个值保存到session */

传统方式:

<%

Request.setAttribute(“username”,”administrator”);

String username = “” + request.getAttribute(“username”);

Session.setAttribute(“myname”,username);

%>

<bean:define>标签

<bean:define id=”myname” name=”username” scope=”request” toScope=”session”>

<%

out.println(session.getAttribute(“myname”);

%>

9<bean:write>标签

<bean:parameter id=”myname” name=”username” value=”administrator” multiple=”true”/> //其中multiple表示可以接受多个参数值

<bean:write name=”myname”/>

10、显示属性值的四种方法

<%

request.setAttribute(“username”,”administrator”);

%>

第一种:

<%

out.println(request.getAttribute(“username”));

%>

第二种:

<%=request.getAttribute(“username”)%>

第三种:

${username}

第四种:

<bean:write name=”username”>

11<bean:write>标签输出对象的属性值

<%

Emp emp = new Emp();

emp.setUsername(“zhangsan”);

emp.setPasswd(“123456”);

%>

<br>${emp.passwd}

<bean:write name=”emp” property=”passwd”>

12<logic:empty>标签

<logic:empty name=”username”>

对不起,usename值为空

</logic:empty>

13<logic:equal>标签

<logic:equal name=”myname” value=”zhangsan”>

<jsp:forward page=”index.jsp/>

</logic:equal>

14<logic:match>标签

<logic:match parameter=”username” value=”admin”>

你具有管理员的权限

</logic:match>

备注:在IE地址栏里如下输入:

http://localhost:8080/web/a.jsp?username = admin

一:常用标签示例

1、转向的几种方式

第一种:

<jsp:forward page=”index.jsp”/></jsp:forward>

第二种:

request.getRequestDispatcher(“index.jsp”).forward(request,response)

第三种:

<logic:forward name=”K”/>

备注:

在配置文件里要有如下配置:

<global-forward>

<forward name=”K” path=”k.jsp”/>

</global-forward>

2<logic:iterator>标签

传统方式

<table border=”1” bgcolor=”red”>

<%

List list = (List)request.getAttribute(“userlist”);

for(int i=0;i<list.size();i++){

String str = (String)list.get(i);

%>

<tr>

<td><%=str%></td>

</tr>

<%}%>

</table>

<bean:iterate>方式

<table border=”1” bgcolor=”red”>

<logic:iterate id=”u” name=”userlist”>

<tr>

<td><bean:write name=”u”%></td>

</tr>

3ApplicationResources.properties为默认的资源文件名称,struts会在其他资源里找不到时,在这个文件里寻找该资源

4、格式化日期

Locale lc = new Locale(“zh”,”CN”);

System.out.println(Local.getDefault().getDisplayCountry());

int datesstyle = DateFormat.FULL;

int timestyle = DateFormat.FULL;

DateFormat df = DateFormat.getDateTimeInstance(datestyle,timestyle,lc);

Date date = new Date();

String str = df.format(date);

System.out.println(str);

5、资源文件的输出

配置文件里配置(ApplicationResource.properties)

title = my project!

jsp文件里输出:

<bean:message key=”title”/>

struts-config.xml里需配置资源文件

6、修改struts属性文件,使属性文件里可以保存中文

windows->Preferences->General->Content Types里的Text->Java Properties File

7、显示资源文件里的错误信息

资源文件里:

username = username cannot be null

form表单里:

ActionErrors errors = new ActionErrors();

ActionMessage message = new ActionMessage(“username”);

errors.add(“username”,username):

jsp文件里:

<html:form action=”/login”>

<html:text property=”username”/><html:errors property=”username”/>

备注:刷新一个jsp页面时,会调用formbean里的reset()方法,可以把一些初始信息放到该方法中

8、鼠标移过的时候颜色发生变化的效果实现

字体颜色发生变化:<tr onmouseover=”this.style.color=’#CF3698’” onmouseout=”this.style.color=’’” onclick=”this.style.color=’blue’”

MsoNormal

分享到:
评论

相关推荐

    struts学习笔记(四)

    在本“Struts学习笔记(四)”中,我们将会深入探讨Struts框架的核心概念、工作原理以及如何实际运用到项目开发中。 首先,Struts框架的核心目标是提供一个结构化的开发环境,将业务逻辑、数据处理和用户界面有效地...

    [摘]Struts 学习笔记之ActionForm

    博客文章"Struts学习笔记之ActionForm - xiaodaoxiaodao——蓝小刀的自言自语 - BlogJava.mht"可能包含更深入的实践示例和经验分享,包括如何处理ActionForm的生命周期、动态Form Beans、国际化等话题。通过阅读这篇...

    struts学习笔记(4)

    ### Struts 学习笔记(4):深入理解 OGNL 表达式与 Struts2 标签 #### 一、OGNL 表达式简介 **OGNL**(Object Graph Navigation Language)是一种功能强大的表达式语言,在 Struts2 框架中扮演着非常重要的角色。...

    Struts学习笔记

    Struts是Java Web开发中的一款经典MVC框架,由Apache...以上是对Struts学习笔记的简要概括,深入学习Struts,不仅可以提升Web开发技能,还能更好地理解MVC模式以及框架的设计思想,对后续学习其他Web框架也会有所帮助。

    struts 学习笔记(全)

    入门级的struts,适合新手入门

    j2ee平台框架Struts学习笔记

    Struts是Java EE平台上的一款流行开源MVC框架,...学习Struts不仅有助于理解MVC模式,也能为其他类似的Web框架打下坚实的基础。通过深入研究和实践,开发者可以充分利用Struts的特性,构建出高效、稳定的Web应用程序。

    Struts 学习笔记 Struts 中扩展JfreeChart 笔记

    Struts是Apache软件基金会下的一个开源框架,主要应用于Java Web应用程序的开发,它提供了一种组织应用程序代码的方式,使得开发者可以更有效地构建基于MVC(Model-View-Controller)架构的Web应用。JfreeChart则是...

    Struts 学习笔记

    本篇学习笔记将深入探讨Struts的核心概念、工作原理以及如何在实际项目中应用Struts来构建高效、可维护的Web应用程序。 一、Struts框架概述 Struts作为经典的MVC框架,它为Java开发者提供了一种组织应用程序逻辑的...

Global site tag (gtag.js) - Google Analytics