Struts2 formBean传值

来源:互联网 发布:最好的seo课程 编辑:程序博客网 时间:2024/05/20 19:16

1.首先创建一个web项目
web项目
2.给该web项目添加Struts2依赖
struts依赖
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
原创粉丝点击