Struts2学习笔记1

1.使用IDEA+Maven开发第一个Struts2.x程序

注意:

①web.xml的配置

<filter>
  <filter-name>struts2</filter-name>
  <filter-class>
    org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
  </filter-class>
</filter>
<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
②新建Struts.xml及其配置(注:Structs.xml一定要放在src目录下)
<!--设置命名空间-->
<package name="root" namespace="/" extends="struts-default">
    <action name="EchoAction" class="ycit.sth.action.EchoAction">
        <result name="echo.page">echo.jsp</result>
    </action>
</package>

2.取得JSP内置对象(ServletActionContext
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
HttpSession session = request.getSession();

3.配置资源文件*.properties
(将所有提示信息写入)
①编写Messages.properties(src目录下),这是全局资源文件类型
{0} {1} 这是占位符

 ②struts.properties定义资源文件

struts.i18n.encoding=UTF-8
struts.locale=zh_CN
struts.custom.i18n.resources=Messages,Pages //可以配置多个资源文件

③读取资源文件
public String execute() throws Exception {  //执行操作
String msg=super.getText("info.msg",new String []{"2021","1月","3日","你好","啊"});//前面是key,后面通过数组来传递
System.out.println(msg);
return "echo.pages";
}
注:要设置一下.properties的编码方式(UTF-8),不然中文显示出来全是问号
资源文件类型:
第一种,全局资源文件类型(最常用),需要在struts.properties配置
第二种,包级别资源文件,只能在一个包下使用(package.properties)
第三种,指定Action的资源文件,为一个Action服务(名称与Action相同)
posted @ 2021-01-03 13:51  我的愿望是如你所愿  阅读(33)  评论(0)    收藏  举报