代码
web.xml
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:/applicationContext-mvc.xml
springmvc
*.action
index.jsp
applicationContext-mvc.xml
com.peng.controller
HelloController
package com.peng.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class HelloController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index.jsp");
modelAndView.addObject("msg", "hello");
return modelAndView;
}
}
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
My JSP 'index.jsp' starting page
This is my JSP page.
${msg}
package com.peng.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/hello.action")
public String helloAction(Model model) {
model.addAttribute("msg", "hello,world!");
return "index";
}
}
characterFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
characterFilter
/*
HttpServletRequest对象的getParameter方法
package com.peng.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@RequestMapping("/showAddUser.action")
public String showAddUser() {
return "addUser";
}
@RequestMapping("/addUser.action")
public String addUser(HttpServletRequest request, Model model) {
String username = request.getParameter("username");
System.out.println(username);
model.addAttribute("username", username);
return "addUser";
}
}
与表单的name值一样的参数String username
参数名来控制
package com.peng.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@RequestMapping("/showAddUser.action")
public String showAddUser() {
return "addUser";
}
@RequestMapping("/addUser.action")
public String addUser(String username, Model model) {
System.out.println(username);
model.addAttribute("username", username);
return "addUser";
}
}
bean对象 User
参数名来控制
package com.peng.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@RequestMapping("/showAddUser.action")
public String showAddUser() {
return "addUser";
}
@RequestMapping("/addUser.action")
public String addUser(User user, Model model) {
System.out.println(user.username);
model.addAttribute("username", user.username);
return "addUser";
}
}
对象中的对象
参数名来控制
gog.age
class Dog{
private String name;
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
}
class User{
private dog;
public void setDog(Dog dog{
this.dog=dog;
}
public Dog getDog(){
return this.dog;
}
}
package com.peng.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@RequestMapping("/showAddUser.action")
public String showAddUser() {
return "addUser";
}
@RequestMapping("/addUser.action")
public String addUser(String username, Model model) {
System.out.println(user.dog.name);
model.addAttribute("username", user.dog.name);
return "addUser";
}
}
2017/12/25这个错误;
2017-12-25这个可以;
@InitBinder
public void InitBinder(ServletRequestDataBinder srdb) {
srdb.registerCustomEditor(java.util.Date.class, new CustomDateEditor(
new SimpleDateFormat("yyyy-MM-dd"), true));
}
a,b,c
@RequestMapping("/addUser.action")
public String addUser(
HttpServletRequest request,
Model model,
Date date,
String like) {
String username = request.getParameter("username");
System.out.println(username + date.toString() + like);
model.addAttribute("username", username);
return "addUser";
}
{a,b,c}数组形式
@RequestMapping("/addUser.action")
public String addUser(
HttpServletRequest request,
Model model,
Date date,
String[] like) {
String username = request.getParameter("username");
System.out.println(username + date.toString() + Arrays.toString(like));
model.addAttribute("username", username);
return "addUser";
}
@RequestMapping("/addUser.action")
public String addUser(
HttpServletRequest request,
Model model,
Date date,
@RequestParam(value = "like", defaultValue = "啥也不喜欢!") String[] likes) {
String username = request.getParameter("username");
System.out.println(username + date.toString() + Arrays.toString(likes));
model.addAttribute("username", username);
return "addUser";
}
改三个地方
jsp中
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
Add User
解析器(必须有id和class)
后台
package com.peng.controller;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class UserController {
@RequestMapping("/showAddUser.action")
public String showAddUser() {
return "addUser";
}
@RequestMapping("/addUser.action")
public String addUser(@RequestParam MultipartFile file) throws IOException {
FileUtils.writeByteArrayToFile(new File(
"C:\\Users\\Administrator.PC-20160710IJPJ\\Desktop\\haha.png"),
file.getBytes());
System.out.println(file.toString());
return "addUser";
}
@InitBinder
public void InitBinder(ServletRequestDataBinder srdb) {
srdb.registerCustomEditor(java.util.Date.class, new CustomDateEditor(
new SimpleDateFormat("yyyy-MM-dd"), true));
}
}
return "redirect:请求"
return "redirect:addUser.action"