8.SpringMVC参数传递

页面参数传递到controller, 可被同名(与页面标签上的name名对应)的参数接收,用request设值,页面再取出来。 注意乱码解决办法: ①如果是get提交,则在tomcat的server.xml里配置中加上URIEncoding=”UTF-8” ②如果是post提交,则在web.xml中配置spring提供的过滤器。

访问结果:

 

中间出现的问题:

解决方法:

springmvc配置文件里加这句话<mvc:default-servlet-handler/>

或者 

在页面上的 js引用路径上改成:

../../js/

 

代码如下:

1.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
    <servlet>
    <!--基础配置有springMVC配置的servlet路径-->
    <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--如果需要加载其他地方的多个springMVC配置文件-->
           <init-param>
               <param-name>contextConfigLocation</param-name>
               <param-value>classpath*:config/SpringMVCAnnotation-servlet.xml</param-value>
               <!--classpath*代表在src下寻找config文件夹再在其中寻找以-servlet.xml文件结尾的文件-->
           </init-param>
           <!--配置加载顺序的,数字越低优先级越高-->
           <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/</url-pattern><!-- 拦截所有请求 -->
    </servlet-mapping>
</web-app>
web.xml

2.SpringMVCAnnotation-servlet.xml

SpringMVCAnnotation-servlet.xml

3.DataController.java

package annotation;
import javax.servlet.http.HttpServletRequest;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestMethod;  
import org.springframework.web.servlet.ModelAndView;  
  
@Controller  
@RequestMapping("/user/data")  
public class DataController {      
    @RequestMapping(value="/addUser")  
    public String addUser(String userName,String age,HttpServletRequest request){  
        request.setAttribute("userName", userName);  
        request.setAttribute("age", age);  
        System.out.println(userName);
        System.out.println(age);
          
        String result ="this is addUser------";  
        return "userManager";  
    }  
      
    @RequestMapping(value="/delUser")  
    public String delUser(){  
        String result ="this is delUser------";  
        return "";  
    }  
    @RequestMapping(value="/toUser")  
    public String toUser(){  
        return "addUser";  
    }  
}  

4.touser.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>     
  <body>  
    <form action="/SpringMVC5/user/addUser" method="post">  
    <h1>SpringMVC注解</h1>   
    <br>  
    ${result }  
    <input type="submit"  value="post请求">  
    </form>  
  </body>  
</html>  

4.addUser.jsp

<%@ 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 = "/SpringMVC6/user/data/addUser";  
        form.method="post";  
        form.submit();  
    }  
</script>  
</head>  
    <body>  
        <h>添加用户</h>  
        <form action="">  
                姓名:<input type="text" name="userName"/>  
                年龄:<input type="text" name="age"/>          
           <input type="button" value="添加" onclick="addUser()">        
        </form>  
    </body>  
</html> 

 

posted @ 2016-08-19 18:06  陈晓猛  阅读(275)  评论(0编辑  收藏  举报