码迷,mamicode.com
首页 > 编程语言 > 详细

springmvc参数传递(一)

时间:2018-01-03 18:38:50      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:intern   ns-3   www   blog   d3.js   content   http   ima   nal   

1.项目结构

技术分享图片

 

2.UserController.java代码

package com.spring;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
/*@RequestMapping("/user")*/
public class UserController {
    @RequestMapping(value="/add.do",method=RequestMethod.POST)
    public String add(HttpServletRequest request,
            HttpServletResponse response,HttpSession session){
        String userNum=request.getParameter("userNum");
        String passWord=request.getParameter("passWord");
        System.out.println("----添加用户信息----");
        System.out.println("用户名:"+userNum);
        System.out.println("密码::"+passWord);
        return "user/success";
    }
    
    @RequestMapping(value="/add1.do",method=RequestMethod.POST)
    public String add1(HttpServletRequest request,
            HttpServletResponse response,HttpSession session){
        String userNum=request.getParameter("userNum");
        String passWord=request.getParameter("passWord");
        System.out.println("----添加用户信息----");
        System.out.println("用户名:"+userNum);
        System.out.println("密码::"+passWord);
        return "user/success";
    }
    
    @RequestMapping(value="/add2.do",method=RequestMethod.POST)
    public String add2(String username,String password){
        
        System.out.println("----添加用户信息----");
        System.out.println("用户名:"+username);
        System.out.println("密码::"+password);
        return "user/success";
    }
    
    @RequestMapping(value="/add3.do",method=RequestMethod.POST)
    public String add3(@RequestParam(value="username")String username,Integer password){
        
        System.out.println("----添加用户信息----");
        System.out.println("用户名:"+username);
        System.out.println("密码::"+password);
        return "user/success";
    }
    
}

注:传参时不必进行类型转换,SpringMVC自动转换

2.视图层代码

①user_add1.jsp代码 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>添加用户信息!!</h1>
    <form action="add1.do" method="post">
        账号:<input type="text" name="userNum"/><br>
        密码:<input type="text" name="passWord"/><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>

②user_add2.jsp代码 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>添加用户信息!!</h1>
    <form action="add2.do" method="post">
        账号:<input type="text" name="username"/><br>
        密码:<input type="text" name="password"/><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>

③user_add3.jsp代码 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>添加用户信息3!!</h1>
    <form action="add3.do" method="post">
        账号:<input type="text" name="username"/><br>
        密码:<input type="text" name="password"/><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>

 3.springmvc-servlet.xml代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        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-3.0.xsd ">
    <!-- Springmvc注解驱动 -->    
    <mvc:annotation-driven />    
    <!-- 扫描器 -->
    <context:component-scan base-package="com.spring" />
    
    <!-- 配置视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/view/"></property>
        <!-- 后缀 -->
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    
</beans>

 

springmvc参数传递(一)

标签:intern   ns-3   www   blog   d3.js   content   http   ima   nal   

原文地址:https://www.cnblogs.com/2016024291-/p/8184859.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!