赞
踩
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE>
<html>
<body>
<a href="param1.do?name=mike&age=20">第一</a>
<a href="param2.do?n=mike&a=20">第二</a>
<a href="param3/mike/20">第三</a>
<form action="param4" method="post">
<h2>测试表单数据</h2>
name:<input type="text" name="name" value=""/><br>
age:<input type="text" name="age" value=""><br>
sex:<input type="radio" name="sex" value="nan">男
<input type="radio" name="sex" value="nv">女<br>
money <input type="text" name="money" value=""><br>
time<input type="date" name="time" value=""><br>
class<input type="text" name="c.name" value=""><br>
<input type="submit" value="提交">
</form>
</body>
</html>
controller.java
package com.nuc.springdemo;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
@Controller
@RequestMapping
@SessionAttributes(value="time")
public class controller {
@RequestMapping("param1.do")
public String getValues(String name,int age) {
System.out.println("name="+name+",age="+age);
return "demo";
}
@RequestMapping("param2.do")
public String getValuems(@RequestParam(value="n")String name,@RequestParam(value="a")int age) {
System.out.println("name="+name+",age="+age);
return "demo";
}
//restful编码风格
@RequestMapping("param3/{name}/{age}")
public String getValuens(@PathVariable() String name,@PathVariable() int age,Model model) {
System.out.println("name="+name+",age="+age);
model.addAttribute("age",age);
return "demo";
}
@RequestMapping("param4")
public String getValuebs(Student s,HttpServletRequest req,Model model) throws ParseException {
System.out.println(s);
System.out.println(req.getParameter("money"));
String time =req.getParameter("time");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(time);
model.addAttribute(date);
return "demo";
}
}
Student.java
package com.nuc.springdemo;
public class Student {
private String name;
private int age;
private String sex;
private Clazz c;
public Student() {
super();
}
public Student(String name, int age, String sex, Clazz c) {
super();
this.name = name;
this.age = age;
this.sex = sex;
this.c = c;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Clazz getC() {
return c;
}
public void setC(Clazz c) {
this.c = c;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", sex=" + sex + ", c=" + c + "]";
}
}
Clazz.java
package com.nuc.springdemo;
public class Clazz {
String name;
public Clazz() {
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Clazz [name=" + name + "]";
}
}
demo.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!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></title>
</head>
<body>
<h1>This is SpringMVC Demo</h1>
${age}<br>
<fmt:formatDate value="${date}" pattern="yyyy-MM-dd"/>
</body>
</html>
springMvc.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"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 配置扫描的包 -->
<context:component-scan base-package="com.nuc.*" />
<!-- 注册HandlerMapper、HandlerAdapter两个映射类 -->
<mvc:annotation-driven />
<!-- 访问静态资源 -->
<mvc:default-servlet-handler />
<!-- 视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。