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>
原文地址:https://www.cnblogs.com/2016024291-/p/8184859.html
时间: 2024-11-03 13:20:00