spring 从jsp到jsp

小例子

1.jar信息

spring 从jsp到jsp

spring 从jsp到jsp

2.web.xml文件配置

spring 从jsp到jsp
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
  <!-- name 自己拟定  -->
  <servlet>
      <servlet-name>test</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>
  
  
  <!-- url-pattern 是对应的*.test的响应进行处理的 -->
  <servlet-mapping>
      <servlet-name>test</servlet-name>
      <url-pattern>*.test</url-pattern>
  </servlet-mapping>
  
</web-app>
spring 从jsp到jsp

3.编写add_emp.jsp(WEB-INF下)

spring 从jsp到jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>add_emp.jsp</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    
    <form  method="post">&nbsp; 
        name:<input name="name" />
        phone:<input name="phone" />
        empNo:<input name="empNo" />
        hireDate:<input name="hireDate" />
        department:
        <select name="dept">
            <c:forEach items="${deptList}" var="dept">
                <option value="${dept }">${dept }</option>
            </c:forEach> 
        </select>
            
        <input type="submit" value="add" />
    
    
    </form>
  </body>
</html>
spring 从jsp到jsp

4.在WEB-INF目录下,配置test-servlet.xml  文件

spring 从jsp到jsp
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<!-- 简单的映射处理 -->
 <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
     <property name="mappings">
         <props>
             <prop key="add_emp.test">addEmpController</prop>
         </props>
     </property>
 </bean>
 
 
  <bean id="addEmpController" class="com.spring.test.EmpFormController">
          <property name="commandClass" value="com.spring.test.Emp" />
        <property name="formView" value="add_emp" />
        <property name="successView" value="success" />
  </bean>
  
   <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
                   <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
                   <property name="prefix" value="/WEB-INF/jsp/" />
                <property name="suffix" value=".jsp" />
    </bean>
</beans>
spring 从jsp到jsp

5.写bean文件 

spring 从jsp到jsp
package com.spring.test;

import java.text.SimpleDateFormat;
import java.util.Date;


public class Emp {

    // 注入的默认是String类型,如果要注入其他类型,则需要数据绑定
    private String name;
    private String phone;
    private int empNo;
    //private Date hireDate;
    private String dept;
    
    public String getDepartment() {
        return dept;
    }

    public void setDepartment(String dept) {
        this.dept = dept;
    }

    /*public Date getHireDate() { 
        return hireDate;
    }

    public void setHireDate(Date hireDate) {
        this.hireDate = hireDate;
    }*/

    public int getEmpNo() {
        return empNo;
    }

    public void setEmpNo(int empNo) {
        this.empNo = empNo;
    }

    public String getName() {
        return name;
    }
    
    public String getPhone() {
        return phone;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public void setPhone(String phone) {
        this.phone = phone;
    }
    
    public String toString()
    {
        return "empNo   " + empNo + "  name  "+name + "    phone " + phone +",  " ;
    }
}
spring 从jsp到jsp

6.处理java文件。

spring 从jsp到jsp
package com.spring.test;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class EmpFormController extends SimpleFormController {

    @Override
    protected ModelAndView onSubmit(Object command) throws Exception {
        // TODO Auto-generated method stub

        Emp emp = (Emp)command;
        System.out.println(emp);
        return super.onSubmit(command);
    }

    @Override
    protected Map referenceData(HttpServletRequest request, Object command,
            Errors errors) throws Exception {
        Map<String,Object> model = new HashMap<String, Object>();
        model.put("deptList",new String[]{"sales","manages"});
        // TODO Auto-generated method stub
        return model;
    }
    
    @Override
    protected void initBinder(HttpServletRequest request,
            ServletRequestDataBinder binder) throws Exception {
            
        binder.registerCustomEditor(Date.class,
                new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }

}
spring 从jsp到jsp

 

spring 从jsp到jsp,布布扣,bubuko.com

spring 从jsp到jsp

上一篇:javascript的函数内部变量的作用域


下一篇:第六次jsp