zoukankan      html  css  js  c++  java
  • spring 从jsp到jsp

    小例子

    1.jar信息

    2.web.xml文件配置

    <?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>

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

    <%@ 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>

    4.在WEB-INF目录下,配置test-servlet.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"
        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>

    5.写bean文件 

    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 +",  " ;
        }
    }

    6.处理java文件。

    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));
        }
    
    }
    珍惜现在,珍惜每一分,每一秒。 从不放弃,坚持。。。。。
  • 相关阅读:
    rabbitmq级联之shovel插件和exchange.bind
    gcc常用编译选项
    windows下rabbitmq-c编译(带openssl、无需MinGW)
    windows下openssl编译
    关于actor模型
    mysql优化之使用iotop+pt-ioprofile定位具体top io文件
    centos6下jbd2进程占用大量IO处理
    c++中的header-only library
    java中线程的停止以及LockSupport工具类
    java Condition条件变量的通俗易懂解释、基本使用及注意点
  • 原文地址:https://www.cnblogs.com/feiguo/p/3669772.html
Copyright © 2011-2022 走看看