1. 程式人生 > >springmvc參數傳遞(一)

springmvc參數傳遞(一)

har enc parameter tle log con println req tex

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參數傳遞(一)