Struts2 formBean传值
来源:互联网 发布:最好的seo课程 编辑:程序博客网 时间:2024/05/20 19:16
1.首先创建一个web项目
2.给该web项目添加Struts2依赖
3.最后项目结构如下:(action、model这些包自己添加)
关键的web.xml,struts.xml如下:
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>StrutsTest</display-name> <!--struts2的核心分发器--> <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>/*</url-pattern> </filter-mapping> <!--欢迎页面--> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list></web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts> <!-- 指定全局资源 --> <constant name="struts.custom.i18n.resources" value="mess"></constant> <!-- 指定编码 --> <constant name="struts.custom.i18n.encoding" value="utf-8"></constant></struts>
3.创建mess.properties(消息属性配置文件)
loginPage=\u767B\u9646\u9875\u9762erroPage=\u5931\u8D25\u9875\u9762succPage=\u6210\u529F\u9875\u9762user=\u7528\u6237\u540Dpass=\u5BC6\u7801login=\u767B\u9646
5.创建登陆页面(index.jsp)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" isELIgnored="false"%><!--引入struts标签--><%@ taglib uri="/struts-tags" prefix="s" %><%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> <!--通过mess.properties中的属性值匹配映射--> <s:text name="loginPage"></s:text> </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> <h3>登录页面</h3> <!--通过form表传递值--> <s:form action="/login"> <s:textfield name="per.userName" key="user"></s:textfield> <s:textfield name="per.passWord" key="pass"></s:textfield> <s:submit key="login"></s:submit> </s:form> </body></html>
6.在strus.xml中配置请求(login.action)的处理类及返回页面映射
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts> <!-- 指定全局资源 --> <constant name="struts.custom.i18n.resources" value="mess"></constant> <!-- 指定编码 --> <constant name="struts.custom.i18n.encoding" value="utf-8"></constant> <!--注意一定要配置namespace--> <package name="log" extends="struts-default" namespace="/"> <action name="login" class="com.action.LoginAction"> <!--结果返回页面映射--> <result name="success">/page/success.jsp</result> <result name="fail">/page/fail.jsp</result> </action> </package></struts>
7.成功页面(success.jsp)和失败页面(fail.jsp)
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib uri="/struts-tags" prefix="s" %><%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> 欢迎:<s:text name="succPage"></s:text> </body></html>
fail.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib uri="/struts-tags" prefix="s" %><%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 'fail.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> Soory:<s:text name="erroPage"></s:text> </body></html>
8.发布该应用到tomcat,通过localhost:8080/StrutsTest进行访问
0 0
- Struts2 formBean传值
- struts formbean
- 动态Formbean
- 动态Formbean
- FormBean到底是什么?
- 使用formbean的心得体会
- 动态formbean例子
- struts FormBean的用处
- formbean的使用
- PO、VO、FormBean
- struts1.x的formbean
- struts1 FormBean 的名字
- 静态和动态FormBean
- struts1.填充formbean分析
- 【Strurs1】FormBean创建错误!
- FormBean类型转换问题
- struts1(13)----动态FormBean
- 自定义formbean验证框架
- 【C++研发面试笔记】3. 命名空间与内存管理
- 进入OS前的两步之PendSV(任务切换)
- OpenCL笔记
- Linux - C数据库编程(预习内容四)
- 嵌入式系统学习(九)-用过内核驱动操作GPIO
- Struts2 formBean传值
- map应用:反片语
- STL(七):stack 与 queue
- 嵌入式Linux移植之initramfs
- linux如何用fdisk命令分区
- 拓扑排序 - Topological Sort
- tomcat热部署实现方法1
- TCP/IP概述
- Android 媒体开发:AVI视频格式解析