struts2 基本
来源:互联网 发布:单片机电子设计与制作 编辑:程序博客网 时间:2024/06/05 05:08
大概流程– 取 调 转
首先jsp页面发出请求,struts2核心拦截器拦截该请求,到struts.xml中找到该请求,去对应的类的对应方法处理该请求,这个类是action类,处理的方法返回一个字符串,这个字符串在struts.xml里面相应的能找到对应的jsp或者其他类型的页面,返回请求响应的页面。一次请求结束……
(大概就是类似一个servlet 的作用,从页面取数据,调用相应的处理方法,转到结果显示页面)
如下配置:
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>
阅读全文
0 0
- struts2 基本
- struts2-struts2的基本配置
- Struts2总结 - 基本配置
- Struts2基本配置实例
- struts2之基本验证
- struts2学习基本内容
- Struts2的基本流程
- struts2的基本配置
- Struts2基本的配置
- struts2的基本流程
- Struts2的基本配置
- Struts2 的基本流程
- struts2的基本用法
- Struts2 基本流程
- Struts2 的基本应用
- Struts2表单基本验证
- Struts2.2基本配置
- struts2基本验证
- Android Sms短信发送
- OpenCV Python 学习笔记(三) 仿射变换
- Mybatis 浅析
- HDOJ2058The sum problem
- 初识javaweb之http协议和tomcat
- struts2 基本
- Spring动态代理用JDK还是用CGLIB?
- merge合并链表
- React Native组件生命周期
- 面向新手的Web服务器搭建(一)——IIS的搭建
- 《数据结构与算法-Python语言描述》读书笔记(8)第8章字典和集合(关键词:数据结构/算法/Python/字典/集合)
- 【Noip模拟】RP字符串
- 通信原理
- java 异常处理 Throwable Error 和Exception