您的位置:首页 > Web前端 > JavaScript

js 选择提交到 action 里面的方法

2011-08-26 13:07 176 查看
1,Myjsp.jsp 页面

注意:m 参数,struts-config.xml 里面的设置与这个参数对应

<html>

<head>

<base href="<%=basePath%>">

<title> JS 提交到Action</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">

-->

<script type="text/javascript">

function tj(){

document.index.action="/riskprj/login.do?m=modi" ;

document.index.submit();

}

</script>

</head>

<body>

<form id="index" name="index" method="post">

<input type="text" id="txtinput" name="username">

<input type="button" id="btnok" name="ok" onclick="tj()" value="提交">

</form>

</body>

</html>

2,LoginAction.java

注意:默认情况下action 继承 Action , 这里要继承 DispatchAction

/*

* Generated by MyEclipse Struts

* Template path: templates/java/JavaClass.vtl

*/

package com.yourcompany.struts.action;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.actions.DispatchAction;

public class LoginAction extends DispatchAction {

/*

* Generated Methods

*/

/**

* 如果存在 execute 方法,执行完其他方法之后,仍然要执行 execute 方法 */

public ActionForward add(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) {

// TODO Auto-generated method stub

return mapping.findForward("success");

}

public ActionForward modi(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) {

// TODO Auto-generated method stub

return mapping.findForward("love");

}

}

3,struts-config.xml

注意,如果要提交到action 里面的某个方法,需要加上parameter="m" ,参数与页面上的参数要对应

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>

<data-sources />

<form-beans />

<global-exceptions />

<global-forwards />

<action-mappings >

<action path="/login" type="com.yourcompany.struts.action.LoginAction" parameter="m">

<forward name="success" path="/home.jsp" />

<forward name="register" path="/MyJsp.jsp" />

<forward name="love" path="/love.jsp" />

</action>

</action-mappings>

<message-resources parameter="com.yourcompany.struts.ApplicationResources" />

</struts-config>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: