spring中的Map集合、视图模型

26、Spring中的用户的添加

注意:1.WEB-INF下的views下的

自己新建即可,在类中return 到jsp文件即可,这里只需要说明每个类即可。

2.springmvc-servlet.xml这个配置文件都是一样的,这里也不需要多写。

3.web.xml这个配置文件也都是一样的,也不需要多写。

4.以下只需要写每一个实现类和一个springmvc-servlet.xml和web.xml文件即可;

26.1 web.xml配置文件如下:

<?xml version="1.0"encoding="UTF-8"?>

<web-app xmlns:xsi="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">

<display-name>springMvc-1</display-name>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

<!--spring的中文乱码解决 -->

<filter>

<filter-name>characterEncodingFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

<init-param>

<param-name>forceEncoding</param-name>

<param-value>true</param-value>

</init-param>

</filter>

<!-- 拦截器 -->

<filter>

<filter-name>hiddenHttpMethodFilter</filter-name>

<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>hiddenHttpMethodFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<!--这个servlet-name的值要和springmvc-servlet.xml的文件名保持一致-->

<servlet>

<servlet-name>springmvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>springmvc</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

</web-app>

26.2 springmvc-serlvet.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: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">

<!--扫描包-->

<context:component-scan base-package="com.ask"/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix"value="/WEB-INF/views/"></property>

<property name="suffix"value=".jsp"></property>

</bean>

</beans>

26.3 访问中的demo.jsp这里只需要写一个即可

如图:demo.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>

<form action="user/demo"method="post">

<table>

<tr><td>username</td><td> <input type="text"name="username"></td></tr>

<tr><td>password</td><td> <input type="password"name="password"></td></tr>

<tr><td>age</td><td> <input type="text"name="age"></td></tr>

<tr><td>sex</td><td> <input type="radio"name="sex"value="1">男 <input type="radio"name="sex"value="0">女</td></tr>

<tr><td><input type="submit"name="submit"value="点击提交"></td></tr>

<!-- 注意这里的提交name或者value要有submit这个值-->

</table>

</form>

</body>

</html>

26.4新建一个demoUser,用户用户的添加;

package com.ask.controller;

import java.io.UnsupportedEncodingException;

import org.springframework.stereotype.Controller;

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

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 org.springframework.web.bind.annotation.SessionAttributes;

import com.ask.controller.entity.User;

//添加地址;

@RequestMapping("/user")

@Controller

public class demoUserController{

//用户的添加的演示;

@RequestMapping(value="/demo")

public String demoUser(User user) throws UnsupportedEncodingException{

String name=new String(user.getUsername().getBytes("iso8859-1"),"utf-8");

user.setUsername(name);

System.out.println(user);

return "user_list";

}

}

26.5 新建一个demo1User的类用于模型转换视图

package com.ask.controller;

import java.io.UnsupportedEncodingException;

import org.springframework.stereotype.Controller;

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

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 org.springframework.web.bind.annotation.SessionAttributes;

import org.springframework.web.servlet.ModelAndView;

import com.ask.controller.entity.User;

//添加地址;

@RequestMapping("/user")

@Controller

public class demo1UserController{

//模型转换视图的演示;

@RequestMapping(value="/demo1")

public ModelAndView demo1User(){

ModelAndView modelAndView=new ModelAndView("user_list1") ;

User u=new User("张三", "123456", 20, 1);

modelAndView.addObject("user",u);

return modelAndView;

}

}

26.6 新建一个demo2User类用于Map集合实现用户添加

package com.ask.controller;

import java.io.UnsupportedEncodingException;

import java.util.Map;

import org.springframework.stereotype.Controller;

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

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 org.springframework.web.bind.annotation.SessionAttributes;

import org.springframework.web.servlet.ModelAndView;

import com.ask.controller.entity.User;

//添加地址;

@RequestMapping("/user")

@Controller

public class demo2UserController{

//通过Map集合的方法;

@RequestMapping(value="/demo2")

public String demo2User(Map<String, Object> map){

User u=new User("tom", "123456", 20, 1);

map.put("user", u);

System.out.println(u);

return "user_list2";

}

}

26.7 新建一个demo3User的类,用于Map集合的用户的添加和属性追加

package com.ask.controller;


import java.io.UnsupportedEncodingException;

import java.util.Map;


import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

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

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 org.springframework.web.bind.annotation.SessionAttributes;

import org.springframework.web.servlet.ModelAndView;


import com.ask.controller.entity.User;

//添加地址;

@RequestMapping("/user")

@Controller

public class demo3UserController{

//通过Map集合的方法;

@RequestMapping(value="/demo3")

public String demo3User(Model map){

User u=new User("tom", "123456", 20, 1);

map.addAttribute("user", u);

System.out.println(u);

return "user_list3";

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值