问题描述
在自学jquery,自己用struts2+jquery进行练习的时候遇到点问题,struts2不跳页...把代码发出来,请帮忙指点下.....---------------------------struts.xml-------------------------------<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"" http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <package name="test" namespace="/test" extends="struts-default"> <action name="test" class="com.javabiz.cn.TestAction" method="test"> <result name="success"> /Myjsp.jsp </result> </action> </package></struts>---------------------------test.js-------------------------------$(document).ready(function(){ $("#userBtt").click(function(){ var username = $("#username").val(); if(username=="") { alert("不能为空"); } else { $.post("http://localhost:8010/jqueryTest/test/test?username="+encodeURI(encodeURI(username))); } });});---------------------------TestAction.java-------------------------------package com.javabiz.cn;import java.io.UnsupportedEncodingException;import java.net.URLDecoder;import com.opensymphony.xwork2.ActionSupport;public class TestAction extends ActionSupport { private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String test() { try { String username2 = URLDecoder.decode(username, "UTF-8"); System.out.println(username2); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "success"; }}---------------------------index.jsp-------------------------------<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%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>Test</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"> <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script> <script type="text/javascript" src="js/test.js"></script> <link rel="stylesheet" type="text/css" href="css/test.css"> </head> <body> <table> <tr> <td>请输入用户名:<input type="text" name="username" id="username"></td> <td><input type="button" value="校验" id="userBtt"></td> <td></td> </tr> </table> </body></html>-----------------------------------------------------------------现在控制台也能打印文本框里输入的值,但是就是不执行跳转,这是为什么啊?如果想跳转的话,应该怎么办?
解决方案
$.post 是ajax提交你想页面跳转使用window.location.href=path