struts2笔记之第二讲

[align=center][size=large]struts2笔记之第二讲[/size][/align]

1、<result>标签的name属性,如果不配置,那么缺省值为success

2、Struts2提供了一个Action接口,在Action接口中定义了一些常量和execute方法
我们可以使用该接口,这样开发更规范

3、struts2的常用配置参数
* struts.configuration.xml.reload
--当struts.xml配置文件发生修改,会立刻加载,在生产环境下最好不要配置
* struts.devMode
--会提供更加友好的提示信息

以上参数配置方式有两种:
* 在struts.properties文件中配置
* 在struts.xml配置文件中

4.例子

实现action接口,execute 方法再也不用担心写错字母而导致运行报错
package com.struts2;

import com.opensymphony.xwork2.Action;
/**
*实现action接口 强制实现接口里面的execute方法
**/
public class LoginAction implements Action {
private String username;
private String password;

/**
* struts2 默认调用这个方法,返回字符串
* @return
* @throws Exception
*/
public String execute() throws Exception{

if("admin".equals(username) && "admin".equals(password)){
return "success";
}else{
return "error";
}

}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}


}


struts.properties文件 加上2个参数
struts.devMode=true
struts.configuration.xml.reload=true


struts.xml文件(如果配置struts.properties中不加上面的2个参数的话,我们也可以加到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>
<!--当struts.xml配置文件发生修改,会立刻加载,在生产环境下最好不要配置-->
<!-- <constant name="struts.configuration.xml.reload" value="true"/> -->
<!--会提供更加友好的提示信息-->
<!-- <constant name="struts.devMode" value="true"/> -->
<!-- 需要继承struts-defaluts包,这样就拥有基本的功能 -->
<package name="struts2" extends="struts-default">

<action name="login" class="com.struts2.LoginAction">
<!-- <result name="success">/login_success.jsp</result> 默认值 sucess 所以可以省略 -->
<result >/login_success.jsp</result>
<result name="error">/login_error.jsp</result>
</action>

</package>

</struts>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值