
//两个js视图 ,一个用于表单写信息,一个用于显示信息
//表单写信息。采用某种方式传递给某个action,action名要对应
//基本类的属性名若对应于form表单的属性名,那么在控制类中的函数使用该基本类时默认已经set过了
//若想将控制类中的值传递给jsp页面显示,那么需要用HttpServletRequest request来setAttribute
//显示界面${}显示
package spring06;
import org.springframework.stereotype.Component;
@Component
public class User {
String name;
String age;
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("User类中的setName方法");
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
System.out.println("User类中的setAge方法");
this.age = age;
}
}
package spring06;
import org.springframework.stereotype.Component;
@Component
public class Product {
String proName;
public String getProName() {
return proName;
}
public void setProName(String proName) {
System.out.println("User类中的setProName方法");
this.proName = proName;
}
public int getProNum() {
return proNum;
}
public void setProNum(int proNum) {
System.out.println("User类中的setPronum方法");
this.proNum = proNum;
}
int proNum;
}
package spring06;
import java.util.List;
public class Tool {
List<User>ls;
public List<User> getLs() {
return ls;
}
public void setLs(List<User> ls) {
this.ls = ls;
}
}
package spring06;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class Ctrl {
@RequestMapping(value="/reg",method=RequestMethod.POST)
String doreg(String username,int num){
System.out.println(username+num);
return "NewFile";
}
@RequestMapping(value="/douser",method=RequestMethod.POST)
String doreg(User user,HttpServletRequest request){
request.setAttribute("username",user.getName());
request.setAttribute("u",user);
System.out.println(user.getName()+user.getAge());
return "NewFile";//redirect:NewFile.jsp
}
@RequestMapping(value="/pro",method=RequestMethod.POST)
String Pro(Product pro,HttpServletRequest request){
request.setAttribute("pro", pro);
return "NewFile";
}
@RequestMapping(value="/tool",method=RequestMethod.GET)
String Tool(Tool tool,HttpServletRequest request) throws UnsupportedEncodingException{
String str=request.getParameter("ls[0].name");
byte[] bytes=str.getBytes("ISO-8859-1");
String name=new String(bytes,"utf-8");
List<User> ls = tool.getLs();
request.setAttribute("ls", ls);
request.setAttribute("swithname", name);
return "NewFile";
}
}
<%@ 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>
<a href="<%=request.getContextPath() %>/reg">lala</a>
<form action="<%=request.getContextPath()%>/reg" method="post">
<input type="text" name="username">
<input type="text" name="num">
<input type="submit" value="reg"><br>
</form><br>
<form action="<%=request.getContextPath()%>/douser" method="post">
<input type="text" name="name">
<input type="text" name="age">
<input type="submit" value="douser"><br>
</form><br>
<form action="<%=request.getContextPath()%>/pro" method="post">
<input type="text" name="proName">
<input type="text" name="proNum">
<input type="submit" value="pro"><br>
</form><br>
<form action="<%=request.getContextPath()%>/tool" method="get">
<input type="text" name="ls[0].name">
<input type="text" name="ls[0].age"><br>
<input type="text" name="ls[1].name">
<input type="text" name="ls[1].age"><br>
<input type="text" name="ls[2].name">
<input type="text" name="ls[2].age">
<input type="submit" value="tool">
</form><br>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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>
user : ${username}<br>
username: ${u.getName()}<br>
userage: ${u.getAge()}<br><br>
List:
<c:forEach items="${ls}" var="a">
${a.getName()}
</c:forEach>
<br><br>
productName:${ pro.getProName()}<br>
productNum:${ pro.getProNum()}<br><br>
<style type="text/css">
tr:HOVER{
background-color:yellow;
}
</style>
<table border="1">
<c:forEach items="${ls }" var="ls">
<tr>
<td>toolname: ${ls.name }</td>
<td>toolage: ${ls.age }</td></tr></c:forEach>
</table><br>
get方式ls[0].name: ${swithname }
</body>
</html>
<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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<context:component-scan base-package="spring06"></context:component-scan>
<!--对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--web directory-->
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
<?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>onlineshop</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>start.jsp</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>
</web-app>

