struts2 基本

来源:互联网 发布:单片机电子设计与制作 编辑:程序博客网 时间:2024/06/05 05:08

大概流程– 取 调 转

首先jsp页面发出请求,struts2核心拦截器拦截该请求,到struts.xml中找到该请求,去对应的类的对应方法处理该请求,这个类是action类,处理的方法返回一个字符串,这个字符串在struts.xml里面相应的能找到对应的jsp或者其他类型的页面,返回请求响应的页面。一次请求结束……
(大概就是类似一个servlet 的作用,从页面取数据,调用相应的处理方法,转到结果显示页面)

Created with Raphaël 2.1.0jsp页面发送请求struts2 获取页面数据根据struts2.xml调用action类里的方法action方法返回字符串根据该返回字符串跳转到结果页面页面显示结果

如下配置:
1、web.xml

<filter>    <filter-name>struts2</filter-name>    <filter-class>org.apache.struts2.dispatcher.ng.filter.    StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping>   <filter-name>struts2</filter-name>   <url-pattern>*.action</url-pattern></filter-mapping>

*.action —- 表示拦截所有以.action结尾的url,然后交给struts2核心拦截器,
这个核心拦截器再根据struts.xml里的配置去找相应的方法处理

2、jsp 发送请求页面

<s:form action="userAction!login.action" method="post">      <s:textfield name="user.name" label="USERNAME"></s:textfield>      <s:textfield name="user.password" label="USERPASSWORD"></s:textfield>      <s:submit value="submit" align="right"></s:submit>    </s:form>

action 这里有三种写法

(1)静态调用login方法:直接写userAction!login
此时在web.xml里面的

 <url-pattern>*</url-pattern>

(2)动态调用login方法:userAction!login.action
Web.xml里面配过滤器

<filter-mapping>  <filter-name>struts2</filter-name>  <url-pattern>*.action</url-pattern></filter-mapping>

此时在struts.xml中需要加入

<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

(3)通配符:userAction!login
在struts.xml里面

<action name="*Action" class="action.UserAction" method=”{1}”>      <result name="success">/{1}_list.jsp</result>  </action>

3、struts.xml

<!DOCTYPE struts PUBLIC        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"        "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>    <include file="struts-default.xml"/><constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>    <package name="user" namespace="/" extends="struts-default" >        <action name="userAction" class="action.UserAction" >            <result name="loginError">/login.jsp</result>             <result name="success">/list.jsp</result>              <result name="update">/update.jsp</result>        </action>    </package></struts>

4、action类

public class UserAction {//从页面上取的值,作为属性,要加get 和 set 方法private User user ;public User getUser() {return user;}public void setUser(User user) {this.user = user;}// 响应 userAction!login.action 该请求public String login() {userDao.userLogin(user.getName(), user.getPassword());return list();}public String list(){userList = userDao.getUserList();return "success";// 根据返回的值去 struts.xml 里面 返回相应视图}}

5、jsp 响应请求–list.jsp

<body>  <div align="center">    <h1>用户信息列表 </h1>    <a href="register.jsp" >ADDUSER</a>     <table >         <thead>            <tr>                <th>USERID</th>                <th>USERNAME</th>                <th>USERNAME</th>                <th colspan="2">OPERATE</th>            </tr>         </thead>        <s:iterator value="userList">            <tr>                <td><s:property value="id"/></td>                <td><s:property value="name"/></td>                <td><s:property value="password"/></td>                <td>                </td>                <td>                </td>            </tr>        </s:iterator>    </table>  </div>  </body>
原创粉丝点击