struts1.X总结(1-10)

struts总结

2009-2-13 向先函 制作

2009-2-19结束

1、路径问题

 

 

b.jsp

<p>

<a href="../a/a.jsp">a.jsp</a>

<a href="../a/b/c/c.jsp">c.jsp</a>

<a href="../a/d/d.jsp">d.jsp</a>

<a href="../e/e.jsp">e.jsp</a>

</body>

 

2、转发问题

 

* String username=req.getParameter("username");

            *  UserManager user=new UserManager();

            * List list=user.addAllUser(username);

            * req.setAttribute("username", list);

            */

           /* req.getRequestDispatcher("/a/b/b.jsp").forward(req, res);

 

3、配置filter应该注意的问题

 

配置成*.jsp

 

4servlet匹配模式

 

*.do

/servlet/*

/servlet/TestServlet

 

5、原始的MVC的做法

接口类

public interface Action {

    public String service(HttpServletRequest req, HttpServletResponse res);

 

}

实现类

public class AddUser implements Action {

 

    public String service(HttpServletRequest req, HttpServletResponse res) {

       // TODO Auto-generated method stub

       System.out.println("-------addUser---------");

      

       String username=req.getParameter("username");

       UserManager user=new UserManager();

       List list=user.addAllUser(username);

       req.setAttribute("username", list);

       return "/a/b/b.jsp";

    }

 

调用action

 

public class TestServlet extends HttpServlet {

 

    @Override

    protected void service(HttpServletRequest req, HttpServletResponse res)

           throws ServletException, IOException {

 

       String reqURI = req.getRequestURI();

       System.out.println(reqURI);

        String path = reqURI.substring(reqURI.indexOf("/", 1));

       path = path.substring(0, path.indexOf("."));

       System.out.println(path);

       String forword = "";

 

       if ("/servlet/addUser".equals(path)) {

           Action action=new AddUser();

           forword=action.service(req, res);

 

       } else if ("/servlet/modifyUser".equals(path)) {

           System.out.println("-------modifyUser---------");

 

       } else if ("/servlet/delUser".equals(path)) {

           System.out.println("-------delUser---------");

 

       }

       req.getRequestDispatcher(forword).forward(req, res);

 

    }

 

更进配置的方法

 

6、实现struts mvc原理

 

6、服务器验证失效

 

  <action

validate="FALSE"

 

 

7、单例的方法

设置:

public class UserManager {

 

    private static UserManager instance = new UserManager();

   

    private UserManager() {}

   

    public static UserManager getInstance() {

       return instance;

    }

   

获取:

UserManager.getInstance().login(username, password);

 

 

8、异常处理的整个流程

 

建立两个异常类

public class PasswordErrorException extends RuntimeException {

 

}

public class UserNotFoundException extends RuntimeException {

 

}

 

业务调用类

 

public class UserManager {

 

    private static UserManager instance = new UserManager();

   

    private UserManager() {}

   

    public static UserManager getInstance() {

       return instance;

    }

   

    public void login(String username, String password) {

       if (!"admin".equals(username)) {

           throw new UserNotFoundException();

       }

       if (!"admin".equals(password)) {

           throw new PasswordErrorException();

       }

    }

   

 

 

Action转发

 

String errorInfo = "";

       try {

           UserManager.getInstance().login(username, password);

           //request.setAttribute("username", username);

           return mapping.findForward("success");

       }catch(UserNotFoundException unfe) {

           unfe.printStackTrace();

           errorInfo = "用户不能找到,用户名称=[" + username + "]";

       }catch(PasswordErrorException pee) {

           pee.printStackTrace();

           errorInfo = "密码错误";

       }

       request.setAttribute("errorinfo", errorInfo);

       return mapping.findForward("error");

    }

 

页面调用

 

失败页面

<title>登录失败</title>

</head>

<body>

    <%=request.getAttribute("errorinfo") %>

 

成功页面

 

<%@ page import="com.bjsxt.struts.*" %>

<title>登录成功</title>

</head>

<body>

    <%

       LoginActionForm laf = (LoginActionForm)request.getAttribute("loginForm");//因为作用域是request

    %>

    <%=laf.getUsername() %>,登录成功

 

  

9、小问题

<action-mappings>

       <action path="/login"

              type="com.bjsxt.struts.LoginAction"

              name="loginForm"

              scope="request"

              validate="false"

              attribute="login"

       >

去数据的时候先会取attribute的数据,参考源码中的 prosser


10
struts的标签库不怎么使用了!是视图技术运行在服务器端的

 

struts标签的配置和使用

 

配置:

    * struts-config.xml文件中加入

    <message-resources parameter="MessageResources" />

    * 拷贝MessageResources.properties文件到src

使用:查考struts-1.2.9-bin/webapps/struts-documentation/userGuide/index

l       采用taglib指令引入 struts.jar当中的META-INF中有

 

服务器端

测试beanwriter

//普通属性

       request.setAttribute("hello", "Hello World");

      

       //html文本

       request.setAttribute("bj", "<font color='red'>北京欢迎您</font>");

      

       //日期

       request.setAttribute("today", new Date());

      

       //数字

       request.setAttribute("n", 123456.987);

      

       //结构

       Group group = new Group();

       group.setName("机电职业技术学院");

      

       User user = new User();

       user.setUsername("张三");

       user.setAge(18);

       user.setGroup(group);

      

       request.setAttribute("user", user);

      

       return mapping.findForward("success");

 

客户端:

 

    <%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%> 

 

<h1>测试BeanWrite</h1>

    <hr>

    <li>普通字符串</li><br>

    hello(jsp脚本):<%=request.getAttribute("hello") %><br>

    hello(标签):<bean:write name="hello"/><br>

    <p>

    <li>html文本</li><br>

    bj(default):<bean:write name="bj"/><br>

    bj(filter="true"):<bean:write name="bj" filter="true"/><br>

    bj(filter="false"):<bean:write name="bj" filter="false"/><br>

    <p>

    <li>格式化日期</li><br>

    today(default):<bean:write name="today"/><br>

    today(format="yyyy-MM-dd HH:mm:ss"):<bean:write name="today" format="yyyy-MM-dd HH:mm:ss"/>

    <p>

    <li>格式化数字</li><br>

    n(default):<bean:write name="n"/><br>

    n(format="###,###.####"):<bean:write name="n" format="###,###.####"/><br>

    n(format="###,###.####"):<bean:write name="n" format="###,###.0000"/><br>

    <p>

    <li>结构</li><br>//标签的执行是在服务器端执行的,不是在客户端执行的,使用双引号没问题

    姓名:<input type="text" value="<bean:write name="user" property="username"/>"><br>

    年龄:<input type="text" value="<bean:write name="user" property="age"/>"><br>

    所属组:<input type="text" value="<bean:write name="user" property="group.name"/>"><br>

    </body>

 

 

测试logic 对象为null就不存在了“”就存在

 

服务器端

 

request.setAttribute("attr1", null);

 

客户端

    <%@taglibprefix="logic"uri="http://struts.apache.org/tags-logic">

 

<h1>测试empty,notEmpty,present,notPresent</h1>

    <hr>

    <logic:empty name="attr1">

       attr1为空<br>

    </logic:empty>

    <logic:notEmpty name="attr1">

       attr1不为空<br>

    </logic:notEmpty>

    <logic:present name="attr1">

       attr1存在<br>

    </logic:present>

    <logic:notPresent name="attr1">

       attr1不存在<br>

    </logic:notPresent>

 

测试Iterate

 

服务器端

 

Group group = new Group();

       group.setName("机电职业技术学院");

      

       List userList = new ArrayList();

       for (int i=0; i<10; i++) {

           User user = new User();

           user.setUsername("user_" + i);

           user.setAge(18+i);

           user.setGroup(group);

           userList.add(user);

       }

      

       request.setAttribute("userlist", userList);

 

客户端

 

<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>   

<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic"%>

    <h1>测试Iterate</h1>

    <hr>

    <li>jsp脚本</li><br>

    <table border="1">

       <tr>

           <td>姓名</td>

           <td>年龄</td>

           <td>所属组</td>

       </tr>

       <%

           List userList = (List)request.getAttribute("userlist");

           if (userList == null || userList.size() == 0) {

       %>

           <tr>

              <td colspan="3">没有符合条件的数据!</td>

           </tr>

       <%

           }else {

              for (Iterator iter=userList.iterator(); iter.hasNext(); ) {

                  User user = (User)iter.next();

       %>

           <tr>

              <td><%=user.getUsername() %></td>

              <td><%=user.getAge() %></td>

              <td><%=user.getGroup().getName() %></td>

           </tr>

       <%

              }

           }

       %>

    </table>

   

    <p>

    <li>标签</li><br>

    <table border="1">

       <tr>

           <td>姓名</td>

           <td>年龄</td>

           <td>所属组</td>

       </tr>

       <logic:empty name="userlist">

           <tr>

              <td colspan="3">没有符合条件的数据!</td>

           </tr>

       </logic:empty>

       <logic:notEmpty name="userlist">

           <logic:iterate id="u" name="userlist">

              <tr>

                  <td>

                     <bean:write name="u" property="username"/>

                  </td>

                  <td>

                     <bean:write name="u" property="age"/>

                  </td>

                  <td>

                     <bean:write name="u" property="group.name"/>

                  </td>

              </tr>

           </logic:iterate>

       </logic:notEmpty>

    </table>  

 

11、(表达式语言)el和(标准标签库)Jstl

 

注意:jstl必须在能够支持j2ee1.4/servlet2.4/jsp2.0版本上的容器才能运行,这个环境是目前较为常用的环境

是不是servlet2.4以上版本看web.xml中的

 

    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 

el表达式的使用

 

服务器

        //普通字符串

       request.setAttribute("hello", "hello world");

      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值