The more things I have, the more lazy I am to write
Struts ' OGNL
ognl object graph navigation language
ognl expression language
page value. But it's too slow, I might as well write it myself
EL expression language: the standard of jsp language (can be used by default)
OGNL Expression Language: Expression language supported by default for Struts tags
Forget it , dad write it himself
Write a JSP page first
${fn:toUpperCase(‘hello’)}
How does this thing work?
1
Write a class first
public class MyFuntion
{
public static String toUpperCase(String str)
{
return str.toUpperCase();
}
}
2
configure this class
In the xml place, write a myfun.tld file
header
<?xmlversion="1.0"encoding="UTF-8"?>
<taglibxmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
< tlib-version > 2.0 </ tlib-version > <!-- tag version -->
< short-name > fn </ short-name > <!-- The prefix of the tag, corresponding to the value of the prefix in taglib -->
<uri>http://qianfeng.com/javaee01</uri>
down is the content
<function>
<name>toUpperCase</name>
<function-class>com.qianfeng.web.function.MyFunction</function-class>
<function-signature>
java.lang.String toUpperCase(java.lang.String)
</ function-signature > (to determine the name of the class)
</function>
</taglib>
3
Well the third step, go to the JSP page
write a
<%@ taglib uri= ” ( this is the address above) ” prefix= ” (this is also written above) fn ” %>
ok you're done.
Write one for yourself.
Okay, it's written
The pit encountered, don't configure it wrong, it's ok , it's very simple
next item
Said to be important? I fell asleep as soon as I spoke, and I skipped my understanding
By the way, a little thing in advance
<!-- By default, the value in "" is regarded as an ognl expression -->
<s:propertyvalue="helloword"/><hr>
<!-- Treat '' as a string -->
<s:propertyvalue="'helloword'"/><hr>
<!-- Can access non-static methods -->
<s:property value="'abcedfg'.length()"/><hr>
<!-- 访问静态属性(+@) -->
<s:property value="@java.lang.Integer@MAX_VALUE"/><hr>
<!-- 使用ognl创建List集合 -->
<s:radio list="#{'1':'男','2':'女'}" label="性别" name="sex"></s:radio>
不想写了自己理解吧
这个ognl标签,也不难(至少我听到的)
先建立个TestAction类 往里面放东西
首先得到和当前线程绑定的actioncontext对象
ActionContext actionContext = ActionConetxt.getContext();
然后分几个map
Session:
Map<String , Object> sessionMap=actionContext.get(“session”);
这个方法太麻烦了啊我还是用下一个吧
HttpSession session = ServletActionContext.getRequest.getSession();
Application:
我也不知道这是个什么玩意从哪来的干嘛的就将就用吧
Map<String , Object> applicationmap=actionContext.get(“application”)
Map<String,Object> applicatMap=actionContext.getApplication();
第二个字节少就用第二个吧我自豪我骄傲我为祖国省能源
applicatMap.put(“这个东西随便起名”,“随便放”);
Request:
Map<String,Object> request=actionContext.get(“request”);
request.put(“”,””);
直接存:
actionContext.put(“”,””);
return “success”
一会试一试这玩意能不能存List集合什么类吧芭啦芭啦
还需要配置Struts.xml
不禁思考如果要从action进要这个东西干嘛,
我直接访问界面看不到,还非要从方法走么,,,
配置xml 和以前一样
JSP页面
例子:
<s:property value=“#session.name”/>
<s:property value=“#key”/>
好了现在去看能不能存List
okok都可以的,直接用最简单那个 ,很吊
我现在想实现的功能是遍历,直接把一个数据库打出来,
拦截器放在一起→ →
还是先声明拦截器
<interceptors>
<interceptor-ref name=“myinterceptor1”class=“”></interceptor-ref>
<interceptor-ref name=“myinterceptor1”class=“”></interceptor-ref>
</interceptors>
用的时候,先定义一个拦截器栈
<interceptors>
<interceptor-stack name=“myStack”>
<interceptor-ref name=“myinterceptor1”>
(设置不拦截的方法)
<param name=“excludeMethods”>login</param>
</interceptor-ref>
<interceptor-ref name=”defaultStack”></interceptor-ref>
</interceptor-stack>
<interceptors>
设置默认的拦截器引用
<default-interceptor-ref name=”myStack”></default-interceptor-ref>
大功告成,感觉有点麻烦 就多写几行代码,,
算了算了 还是用这个吧。
文件的上传与下载
先写上传
上传先写JSP页面(我一直不能独立写出来的一个破东西。。)
不过现在能了,因为只有两行代码(微笑脸)
不过估计还是不能。。。
jsp用的还是Struts标签
<s:form action=”upload.action” enctype=”multipart/form-data”>
(敲黑板这个enctype里的话把我一顿坑,要记下来
enctype=”multipart/form-data”enctype=”multipart/form-data”
enctype=”multipart/form-data”enctype=”multipart/form-data”
enctype=”multipart/form-data”enctype=”multipart/form-data”>
就先写这么多吧
<s:textfield name=”username” label =”用户名”></s:textfield>
<s:file name=”photo” label=”照片”></s:file>
<s:submit value=”提交”></s:submit>
然后开始写action里的方法
public class UploadAction extends ActionSupport
敲黑板:有四个属性
private String username;
private File photo;
private String photoFileName;
private String photoContextType;
public String upload()
{
String path =SevlectActionContext.getServletContext().getRealPath
后面跟个文件夹名~一般是(“/WEB-INF/upload”);
File dir = new File(path);
if(!dir.exists())
{
dir.mkdirs();
}
photo.renameTo(new File(dir,photoFileName));
return success;
}
好了 完成了 很少吧 不用写什么痛苦的流输入~
写四个属性
第一个照片名字(输入的)第二个照片名(输入的)
第三个存储文件文件名(照片名+FileName)
第四个(照片名+ContextType)
都有getset方法
然后获得一个路径
String path 很简单的 ServletActionContext.getServletContext.getRealp
然后创建文件了
File dir = new File(path)
如果dir 没有 就创建一个
然后
photo.renameTo(new File(dir,photoFileName));
接着我们来xml文件里的东西
<package name="p1" extends="struts-default">
<action name="upload" class="com.qianfeng.web.action.UploadAction" method="upload">
<!-- 设置上传文件的扩展名类型 -->
<!-- <interceptor-ref name="defaultStack">
<param name="fileUpload.allowedExtensions">jpg</param>
</interceptor-ref> -->
<!-- 设置上传文件的mime类型 -->
<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedTypes">image/jpeg,image/gif,image/</param>
</interceptor-ref>
<result name="success">/success.jsp</result>
<result name="input">/upload.jsp</result>
</action>
也特别简单我就不写了,不过一个有意思的是这个
<constant name=”struts.multipart.maxSize” value=”5222222”>
</constant>
ok差不多了 我先写一个看看
还没写明白一个迷一样的bug
不过不管了
写download
public class DownLoadAction extends ActionSupport
{
private InputStream inputstream;
public String download() throws FileNotFoundException
{
//设置被下载文件的路径
String path=ServletActionContext.getServletContext.getRealPath
//创建读取被下载文件的字节读取流
inputstream = new FileInputStream(path);
return “success”
}
结束了,,真简单啊。。。
记得写getset方法
struts.xml
<result type=”stream”>
<param name=”inputName”>inputstream</param>
<param name=”contectType”>application/octet-stream</param
<param name=”contenDisposition”>attachment;filename=aa.jp
</result>
okok迷迷糊糊完成了,现在完成一个另一个东西
#Invalid field value for field "birthday".
invalid.fieldvalue.birthday=\u8BF7\u8F93\u5165\u6B63\u786E\u7684\u65E5\u671F\u683C\u5F0Fyyyy-MM-dd
这个是回显设置字体那个
挺好玩
在实体类下
键一个 User.properties文件
里面就写的这个