Struts基于对象的类型转换

逃离我推掉我的手 2021-07-24 23:35 412阅读 0赞

一 视图

1 input.jsp

  1. <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
  2. <%@taglib prefix="s" uri="/struts-tags"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  4. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head>
  7. <title>利用OGNL的进行类型转换</title>
  8. </head>
  9. <body>
  10. <h3>利用OGNL的进行类型转换</h3>
  11. <s:form action="login">
  12. <!-- 该表单域封装的请求参数名为user.name -->
  13. <s:textfield name="user.name" label="用户名"/>
  14. <!-- 该表单域封装的请求参数名为user.pass -->
  15. <s:textfield name="user.pass" label="密码"/>
  16. <tr>
  17. <td colspan="2"><s:submit value="转换" theme="simple"/>
  18. <s:reset value="重填" theme="simple"/></td>
  19. </tr>
  20. </s:form>
  21. </body>
  22. </html>

2 welcome.jsp

  1. <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
  2. <%@taglib prefix="s" uri="/struts-tags"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  4. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head>
  7. <title>转换结果</title>
  8. </head>
  9. <body>
  10. <s:actionmessage/>
  11. 用户名为:<s:property value="user.name"/><br/>
  12. 密码为:<s:property value="user.pass"/><br/>
  13. </body>
  14. </html>

二 配置文件

  1. <?xml version="1.0" encoding="GBK"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <struts>
  6. <package name="lee" extends="struts-default">
  7. <action name="login" class="org.crazyit.app.action.LoginAction">
  8. <result>/WEB-INF/content/welcome.jsp</result>
  9. <result name="error">/WEB-INF/content/welcome.jsp</result>
  10. </action>
  11. <action name="*">
  12. <result>/WEB-INF/content/{1}.jsp</result>
  13. </action>
  14. </package>
  15. </struts>

三 领域类

  1. package org.crazyit.app.domain;
  2. public class User
  3. {
  4. private String name;
  5. private String pass;
  6. // name的setter和getter方法
  7. public void setName(String name)
  8. {
  9. this.name = name;
  10. }
  11. public String getName()
  12. {
  13. return this.name;
  14. }
  15. // pass的setter和getter方法
  16. public void setPass(String pass)
  17. {
  18. this.pass = pass;
  19. }
  20. public String getPass()
  21. {
  22. return this.pass;
  23. }
  24. }

四 action

  1. package org.crazyit.app.action;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. import org.crazyit.app.domain.*;
  4. public class LoginAction extends ActionSupport
  5. {
  6. // 使用User类型的成员变量封装请求参数
  7. private User user;
  8. private String tip;
  9. // user的setter和getter方法
  10. public void setUser(User user)
  11. {
  12. this.user = user;
  13. }
  14. public User getUser()
  15. {
  16. return this.user;
  17. }
  18. public String execute() throws Exception
  19. {
  20. // 通过user的name属性和pass属性来判断控制逻辑
  21. if (getUser().getName().equals("crazyit.org")
  22. && getUser().getPass().equals("leegang") )
  23. {
  24. addActionMessage("登录成功");
  25. return SUCCESS;
  26. }
  27. addActionMessage("登录失败!!");
  28. return ERROR;
  29. }
  30. }

五 测试

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NoZW5ncWl1bWluZw_size_16_color_FFFFFF_t_70

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NoZW5ncWl1bWluZw_size_16_color_FFFFFF_t_70 1

发表评论

表情:
评论列表 (有 0 条评论,412人围观)

还没有评论,来说两句吧...

相关阅读