JAVA的三大框架只是对Spring熟悉点,Struts与hibernate在项目中还未使用过,还是有必要了解下基本知识。
先看Struts一个简单的例子
在web.xml的配置:
<!--定义Struts 2核心Filter--> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>struts.i18n.encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter>
<filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping> struts.xml的配置:<!--让Struts 2的核心Filter拦截所有请求-->
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 指定全局国际化资源文件 --> <constant name="struts.custom.i18n.resources" value="mess" /> <!-- 指定国际化编码所使用的字符集 --> <constant name="struts.i18n.encoding" value="UTF-8" /> <!-- Action的定义 --> <package name="lee" extends="struts-default"> <!-- Action指向的类文件 --> <action name="login" class="app.action.LoginAction"> <!-- 定义从类返回的逻辑视图与物理资源之间的映射 --> <result name="input">/login.jsp</result> <result name="error">/error.jsp</result> <result name="success">/welcome.jsp</result> </action> </package> </struts>
资源文件mess.properties:对于资源文件,需要进行unicode处理loginPage=\u767B\u9646\u9875\u9762 errorPage=\u9519\u8BEF\u9875\u9762 succPage=\u6210\u529F\u9875\u9762 failTip=\u5BF9\u4E0D\u8D77\uFF0C\u60A8\u4E0D\u80FD\u767B\u9646\uFF01 succTip=\u6B22\u8FCE\uFF0C{0},\u60A8\u5DF2\u7ECF\u767B\u9646\uFF01 user=\u7528\u6237\u540D pass=\u5BC6 \u7801 login=\u767B\u9646
物理视图Login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <s:form action="login"> <s:textfield name="username" key="user" /> <s:textfield name="password" key="pass" /> <s:submit key="login" /> </s:form> </body> </html>
另外的文件省略welcome.jsp、errror.jsp处理Action类:
package app.action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport{ private String username; private String password; 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; } public String execute(){ if(getUsername().endsWith("admin")&&getPassword().endsWith("admin")){ ActionContext.getContext().getSession().put("user", getUsername()); return "success"; }else{ return "error"; } } }
关于Struts的简单流程,这幅图解释的不错,拿来主义 贴在这里
常用配置:
struts.properties:管理框架中的常量
struts.i18n.encoding:指定web应用的默认编码集,默认值为UTF-8
当设定参数为GBK时,相当于执行了HttpServletRequest.setCharacterEncoding("GBK")
struts.action.extension:配置需要Struts 2请求的后缀,默认值为action,需要配置多个请求后缀,以(,)隔开,配置action,do即*.action与*.do的请求都由Struts 2处理。
同样 关于常量的配置还可以通过struts.xml中的
<!-- 指定全局国际化资源文件 --> <constant name="struts.custom.i18n.resources" value="mess" />
或者web.xml中的StrutsPrepareAndExecuteFilter时配置为了保持WebWork的向后兼容性,一般用struts.xml中配置<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>struts.i18n.encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter>
Struts框架常量加载的顺序,后面会覆盖前面的值
struts-default.xml-struts-plugin.xml-struts.xml-struts.properties-web.xml
当struts.xml过大是,可以导入配置文件
<include file="struts-part1.xml"></include>
简单配置照着书本就这样,不过Action部分和其他逻辑部分还未提及。