您的位置:首页 > 编程语言 > Java开发

Struts2基本配置

2014-07-01 13:02 106 查看
配置中央控制器

web.xml

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

配置struts.xml



<!--加入验证文件-->



<struts>

<package name=”test” extends=”struts-default”>

<action name=”hello” [method=”方法名] class=”完全限定名”>

<result [name=”返回值]/>/url</result>//不写name值的时候值就是success

</action>

</package>
</struts>



总结:

1. 导入需要的jar

2. 配置web.xml中配置struts2中央控制器(过滤器)

3. 创建需要的struts.xml文件

4. 创建Action

5. 配置struts.xml

注意:配置文件最起码要有<struts></struts>


Action



public class HelloAction{

public String Hello(){

return SUCCESS

}
}




Action中的方法调用

1. 默认会执行Action中的execute方法(开发中常用来页面加载)

2. 通过配置struts.xml中配置,来指定执行的方法(开发中常用来调用业务逻辑方法)

3. url中通过hello!find.action 会执行helloAction方法

4. 通过通配符的形式来调用的方法

1) <action name=”hell*” class=”完全限定名” method=”{1}”>

2) <action name=”*_*” class=”完全包名.{1}Action” mathod=”{2}”>







修改struts2的默认配置(以后缀名为例)



1.Class根目录下建立键值对配置文件struts.properties

struts.action.extension = [后缀1],[后缀2],[后缀3],//修改后缀名

2.struts.xml中配置

<constant name=”struts.action.extension” value=”[后缀名1],[后缀名2],[后缀名3]”/>

备注:两个配置同时写的时候struts.properies的优先级更高







常用的struts2中的跳转类型



1. dispatcher 以请求转发的形式去jsp (常用)

2. redirect 以从定向的形式去jsp //jsp被隐藏,所以不能用这种方式访问

3. chain 以请求转发的形式去Action

4. redirectAction 以重定向的形式去Action (常用)





例子



struts.xml

<!--根据完全限定名查找这个类下面名字为 method loginFrom的业务逻辑方法-->

<action name=”login” method=”loginFrom” class=”完全限定名” >

<!--不设置resulttype 属性的话默认为dispatcher(请求转发)-->

<result [type=”dispatcher”]>/WEB-INF/views/.jsp</result>
</action>

<action name=”logins” method=”login” class=”完全限定名” >

<result name=”success” type=”redirectAction”>url</result>

<!--标准写法-->

[
<result name=”success” type=”redirectAction”>

//不写actionName的时候result里面写的值会自动加入到actioniName中。

<param name=”actionName”>url</param>
//namespace为跳转URL的命名空间
<param name=”namespace”>/模块名</param>
</result>
]
<result name=”error” type=”redrection”>
<param name=”actionName”>url</param>
<param name=”code”>${code }</param>
</reesult>
</action>





Action



Private String username;

Private String password;

Private User user;

Public String loginForm(){

return SUCCESS;

}

Public String login(){



return SUCCESS;

}

//----------get set----------



get获取表单中的值和url中的值

JSP提交



<!--提交基本数据类型的值-->

<input type=”text” name=”username”/>

<input type=”password” name=”password”/>



<!--提交对象的值-->

<input type=”text” name=”user.username”/>

<input type=”password” name=”user.password”/>



JSP获取



<!—获取基本数据类型的值-->

${username }

${password }



<!—获取对象的值-->

${user.username }

${user.password }

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: