Struts2入门学习心得

看这个心得前不知道您之前有没有用过WebWork2,如果用过webwork的同志们估计就不用看了,感觉Struts2和webwork用起来基本一模一样。由于我也是刚开始玩struts2,所以只是初步运用,写得不好请大家见谅。有关Struts2的标签库应用部分不做介绍及评论。

我们首先来搭建一个空的Struts2 Web工程,在IDE中创建一个JAVAEE5的web工程(请确定是JAVAEE5的,不然还需要导入其他jar文件)。创建后请根据自己的需要将以下jar包copy到lib中:

xwork-2.1.2.jar,struts2-core-2.1.6.jar,spring-test-2.5.6.jar,ognl-2.6.11.jar,junit-3.8.1.jar,freemarker-2.3.13.jar,commons-logging-1.1.jar,commons-io-1.3.2.jar,commons-fileupload-1.2.1.jar

OK,接下来在web.xml中加载Struts2应用(这部分可以直接copy用):

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

到现在为止,我们已经成功的将Strust2加载到了新建的工程里去了。

接下来让我们看看配置文件,我们直接在src下创建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="false" /> <!-- 是否为Struts的开发模式 -->
<constant name="struts.url.http.port" value="9999" /> <!-- 这里是确定服务器端口号,我的是9999 ~~ -->
<constant name="struts.i18n.reload" value="true" /> <!-- 允许国际化文件重载 -->

<constant name="struts.locale" value="zh_CN" /><!-- 这两条是确定工程字符集,我的默认是utf-8 -->
<constant name="struts.i18n.encoding" value="UTF-8" />

<!-- Add packages here -->

<package name="default" extends="struts-default">
<action name="showSongs" method="showSongs" class="action.SongAction">
<result name="success">index.jsp</result> <!-- 当方法返回的是一个jsp页面的时候用前面这种方式 -->
</action>

<action name="lookLyric" method="lookSong" class="action.SongAction">
<result name="success">showLyric.jsp</result>
</action>

<action name="addSong" method="insertSong" class="action.SongAction">

<!-- 当方法执行结束后返回到的是另一action时,用下面这种方式,这也是我用到现在感觉和webwork唯一不太一样的地方-->
<result type="redirectAction">showSongs</result>
</action>

<action name="editSong" method="editSong" class="action.SongAction">
<result name="success">editSong.jsp</result>
</action>

<action name="saveSong" method="saveSong" class="action.SongAction">
<result type="redirectAction">showSongs</result>
</action>

<action name="deleteSong" method="deleteSong" class="action.SongAction">
<result type="redirectAction">showSongs</result>
</action>

</package>


</struts>

OK,其实这里的struts.xml就相当于我们用webwork中的xwork.xml、struts1中的struts-config.xml 。

简单介绍一下这个action配置的意思:

<action name="deleteSong" method="deleteSong" class="action.SongAction">
<result type="redirectAction">showSongs</result>
</action>

name:既我们提交请求的方法名,等同于Struts1中的name

class:方法所在的类

一般来说一个action下都有一个默认为excute()的实现类,那么这样我只可以在这个类中声明一个方法,就像在用Struts1时我们会选择这个ActionBean继承的是普通的Action还是MappingDispatchAction抑或是DispatchAction,如果继承了普通的Action那么该类中便只能有一个对应的excute()方法,其他方法为无效,反而如果继承MappingDispatchAction的话我们的ActionBean中便可以书写并使用多个方法了。

method:该类中对应的方法名

接下来写一个简单的ActionBean我们来试一下,上面我的配置文件中声明了一个名为 editSong的Action,那么我就用这个吧:

//如果用过webwork的话这块其实是个webwork一样的

//Struts2与webwork一样,自身并不继承HttpServlet,那么取而代之的

//是com.opensymphony.xwork2.ActionSupport类

//我们完全可以把这个ActionBean当成一个javaBean来使用,而它自身又是一个方法的实现类。

public class SongAction extends ActionSupport{
private int songId;

public int getSongId() {
return songId;
}
public void setSongId(int songId) {
this.songId = songId;
}


public String editSong(){

//是不是用Struts2便不支持使用HttpServlet了呢?当然不是,呵呵:
HttpServletRequest request = ServletActionContext. getRequest();
SongManager songManager = new SongManager();

//这里的songId我们便不用像Struts1中通过HttpRequest获取了
Song song = songManager.getById(songId);

request.setAttribute("song", song); //这个老套路您一定天天写吧,改不了还可以接着用嘛。
return SUCCESS;

//这里返回了一个常量SUCCESS,那么配置文件中的<result name="success">便是它了,其实我们也可以返回一个普通的字符串,例如:

return "su"; 这样也是完全可以的,OK 到此struts2已经可以用了,大家去试试吧
}

}

你可能感兴趣的:(struts2)