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>
注意: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>
相关文章推荐
- js实现表单多按钮提交action的处理方法
- js 提交form表单时,action的动态修改方法
- JS左边select内容选择到右边select内容里面的js实现方法(可以多选和全选)
- 左边select内容选择到右边select内容里面的js实现方法(可以多选和全选)
- 提交form表单后,先进行输入的校验。客户端(将方法写在js里面)
- js实现点击图片自动提交action的简单方法
- 左边select内容选择到右边select内容里面的js实现方法(可以多选和全选)
- 左边select内容选择到右边select内容里面的js实现方法(可以多选和全选)
- js实现点击图片自动提交action的简单方法
- js实现表单多按钮提交action的处理方法
- js与jquery回车提交的方法
- js动态生成form 并用ajax方式提交的实现方法
- 使用<a>标签提交action会执行两次后台方法的原因(反射机制小实例问题的解决)
- ASP.NET MVC中在Action获取提交的表单数据方法总结
- php里面12小时制和24小时制的选择方法
- js数组合并(一个数组添加到另一个数组里面)方法
- js给网页加上背景音乐及选择音效的方法
- Js 阻止表单提交方法
- Angularjs $scope 里面的$apply 方法 和 $watch 方法
- AngularJs $scope 里面的$apply 方法和$watch方法