接下来学习一下Struts2简单的类型转换,Struts2基于ognl.jar实现了简单类型的数据转换。好比jsp页面中的form值与字段值的转换,下面写一个例子。css
一、建立一个jsp页面,编写一个form表单模拟登陆,有一个用户名username和密码password,在加入一个登陆按钮。如图:html
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <s:form name="login" method="post" action="login" namespace="/"> <s:textfield label="username" name="username"></s:textfield> <s:textfield label="password" name="password"></s:textfield> <s:submit value="sgin in"></s:submit> </s:form> </body> </html>
jsp代码中使用了struts2的标签库,标签库的.tld文件在struts2-core-2.3.24.1.jar/META-INF路径下。java
二、建立LoginAction,为了能够将jsp中表单的自动转换提取到变量中,须要在action文件里创建两个字段,并提供get/set方法用于接收:apache
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; } private String username; private String password;
其超类ActionSupport提供了验证功能,须要重写超类的validate()方法,对表单中的数据进行校验,并调用addFieldError()方法记录错误信息,当验证失败,会返回INPUT做为Result,因此须要在struts2.xml中配置返回值为input的result节点,LoginAction类具体代码以下:less
package cn.net.bysoft.lesson2; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { /** * */ private static final long serialVersionUID = -549393995867033046L; @Override public String execute() throws Exception { // TODO Auto-generated method stub if ("admin".equals(getUsername()) && "password".equals(getPassword())) { return SUCCESS; } else { addFieldError("username", "Sorry,wrong username or password,please login again"); return INPUT; } } @Override public void validate() { // TODO Auto-generated method stub if (getUsername().length() == 0) { addFieldError("username", "username is required"); } if (getPassword().length() == 0) { addFieldError("password", "password is required"); } } 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; } private String username; private String password; }
三、配置struts2.xml的内容:jsp
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="struts2_3_24_1" extends="struts-default"> <action name="helloWorld" class="cn.net.bysoft.lesson1.HelloWorldAction"> <result>lesson1/hello.jsp</result> </action> <action name="login" class="cn.net.bysoft.lesson2.LoginAction"> <result name="success">lesson2/success.jsp</result> <result name="input">lesson2/login.jsp</result> </action> </package> </struts>
四、最后当登陆成功时,跳转到success.jsp页面。若login.jsp表单中输入的数据格式有误,则进行提示,如图所示,当用户名或密码为空时,提示:ide
当用户名密码错误时提示(这里默认用户名为admin,密码为password为正确的信息):post
当用户名和密码正确时,跳转到success.jsp:学习
success.jsp的代码以下:ui
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'success.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> username: <s:property value="username"/><br> password: <s:property value="password"/><br> </body> </html>