67,513
社区成员
发帖
与我相关
我的任务
分享
package com.rfid.annotion;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/user/data")
public class SpringAnntion {
@RequestMapping("/addUser")
public String addUser(String userName,String password,HttpServletRequest request){
request.setAttribute("userName", userName);
request.setAttribute("password", password);
return "/userManager";
}
@RequestMapping("/delUser")
public String delUser(HttpServletRequest request){
String result ="this is delUser------优化版";
request.setAttribute("result", result);
return "/jquery";
}
@RequestMapping("/toUser")
public String toUser(HttpServletRequest request){
return "/addUser";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function addUser(){
var form = document.forms[0];
form.action = "/springMVC2/user/data/addUser";
form.method = "get";
form.submit();
}
</script>
</head>
<body>
<form action="">
用户名 :<input type="text" name="userName" />
密码:<input type="password" name="password" />
<input type="button" value="提交" onclick="addUser"/>
</form>
</body>
</html>
[code=text]
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>springMVC1</display-name>
<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>
<servlet>
<servlet-name>springMVC2</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>-->
<servlet-mapping>
<servlet-name>springMVC2</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
[code=text]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 注解扫描包 -->
<context:component-scan base-package="com.rfid.annotion" />
<!-- 开启注解 -->
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<!-- 静态资源访问 -->
<mvc:resources location="/img/" mapping="/img/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<mvc:resources location="/css/" mapping="/css/**"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
<form action="${pageContext.request.contextPath}/你的路径">
</form>
很明显路径不对
<script type="text/javascript">
function addUser(){
var form = document.forms[0];
form.action = "/springMVC2/user/data/addUser";
form.method = "get";
form.submit();
}
</script>
这里的路径错了吧。。应该写成springMVC2/user/data/addUser
我这么看你的代码写的没什么问题啊。。应该就是路径错了吧