J2EE学习笔记——Struts2多方法实现(1)(2)
分享于 点击 4090 次 点评:174
Struts.xml
[html] view plaincopy
- <?xmlversionxmlversion="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>
- <packagenamepackagename="default"extends="struts-default">
- <actionnameactionname="loginAction"class="xuyan.com.action.LoginAction">
- <resultnameresultname="success">/Success.jsp</result>
- <resultnameresultname="error">/Login.jsp</result>
- </action>
- <actionnameactionname="regAction"class="xuyan.com.action.LoginAction"method="Login">
- <resultnameresultname="success">/RegSuccess.jsp</result>
- <resultnameresultname="error">/reg.jsp</result>
- </action>
- </package>
- </struts>
Reg.jsp:
[html] view plaincopy
- <%@ 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 'reg.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">
- </head>
- <body>
- <s:form action="regAction" method="post">
- <s:textfield name="username" label="用户名" /> <br>
- <s:password name="password" label="密码"/> <br>
- <input type="submit" value="注册"> <br>
- </s:form>
- </body>
- </html>
注意:
[html] view plaincopy
- <actionnameactionname="loginAction"class="xuyan.com.action.LoginAction">
- loginAction与login. JSP页面中的<s:form action="loginAction" method="post">对应
- <action name="regAction" class="xuyan.com.action.LoginAction" method="Login">
- regAction与reg.JSP 页面中的 <s:form action="regAction" method="post">对应
- loginAction 类中public String Login()方法必须在Struts.xml中声明
- <action name="regAction" class="xuyan.com.action.LoginAction" method="Login">
- <result name="success">/RegSuccess.jsp</result>
- <result name="error">/reg.jsp</result>
- </action>
用户点评