spring中的RequestMapping用户的添加

25.1 新建一个UserController类

package com.ask.controller;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;

import com.ask.controller.entity.User;

//添加地址;

@RequestMapping("/user")

@Controller

public class UserController{

@RequestMapping(value="/add")

public String getUser(@RequestParam(value="username") String username,

@RequestParam(value="password") String password,

@RequestParam(value="age") int age,

@RequestParam(value="sex") String sex){

System.out.println("username "+username+" password"+password+" age"+age+" sex"+sex);

return "user_list";

}

}

25.2 新建一个springmvc-servlet.xml的配置文件,用来解析最后要输出的jsp网页

"1.0" encoding="UTF-8"?>

"http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

"com.ask"/>

"org.springframework.web.servlet.view.InternalResourceViewResolver">

"prefix" value="/WEB-INF/views/">

"suffix" value=".jsp">

25.3 新建一个user_list.jsp文件

<%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%>

"Content-Type" content="text/html; charset=utf-8">

Insert title here

用户添加成功


25.4 在web.xml配置文件中配置如下:

"1.0" encoding="UTF-8"?>

"http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

springMvc-1

index.html

index.htm

index.jsp

default.html

default.htm

default.jsp

characterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceEncoding

true

hiddenHttpMethodFilter

org.springframework.web.filter.HiddenHttpMethodFilter

hiddenHttpMethodFilter

/*

springmvc

org.springframework.web.servlet.DispatcherServlet

1

springmvc

/

25.5 新建一个index.jsp文件

<%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%>

"Content-Type" content="text/html; charset=utf-8">

Insert title here

"user/add" method="post">

username "text" name="username">

password "text" name="password">

age "text" name="age">

sex "radio" name="sex">男 "radio" name="sex">女

"submit" name="submit" value="点击提交">

你可能感兴趣的:(Spring)