小例子

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));
} }

spring 从jsp到jsp的更多相关文章

  1. Spring Boot 中使用jsp

    接SpringBoot 快速入门(Eclipse): 步骤一:视图支持 Springboot的默认视图支持是Thymeleaf,但是Thymeleaf我们不熟悉,我们熟悉的还是jsp. 所以下面是讲解 ...

  2. [原创]Spring boot 框架构建jsp web应用

    说明 Spring boot支持将web项目打包成一个可执行的jar包,内嵌tomcat服务器,独立部署 为支持jsp,则必须将项目打包为war包 pom.xml中设置打包方式 <packagi ...

  3. Spring Boot入门——使用jsp

    使用步骤: 1.创建Maven web project项目 2.在pom.xml文件中添加依赖 <project xmlns="http://maven.apache.org/POM/ ...

  4. Spring Boot MVC 使用 JSP 作为模板

    Spring Boot 默认使用 Thymeleaf 作为模板引擎,直接在 template 目录中存放 JSP 文件并不能正常访问,需要在 main 目录下新建一个文件夹来存放 JSP 文件,而且需 ...

  5. Spring Security教程之Jsp标签(八)

    目录 1.1     authorize 1.2     authentication 1.3     accesscontrollist Spring Security也有对Jsp标签的支持的标签库 ...

  6. Spring boot之添加JSP支持

    大纲 (1) 创建Maven web project: (2) 在pom.xml文件添加依赖 (3) 配置application.properties支持jsp (4) 编写测试Controller ...

  7. Spring Security教程之Jsp标签(四)

    目录 1.1     authorize 1.2     authentication 1.3     accesscontrollist Spring Security也有对Jsp标签的支持的标签库 ...

  8. Spring boot无法显示jsp页面问题汇总

    问题1: o.s.w.s.r.ResourceHttpRequestHandler:Path with "WEB-INF" or "META-INF": [WE ...

  9. Spring MVC 下index.jsp访问

    spring-mvc.xml配置 <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> <bean class="org.springframework.we ...

  10. JSP以及JSP解析原理

    什么是JSP? JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP这门技术的最大的特点在于,写jsp就像在写htm ...

随机推荐

  1. hdu5379||2015多校联合第7场1011 树形统计

    pid=5379">http://acm.hdu.edu.cn/showproblem.php? pid=5379 Problem Description Little sun is ...

  2. java中finalkeyword

    在java中有3个地方须要用finalkeyword: 1.假设一个不希望被继承,那么用final来修饰这个类 2.假设一个方法不须要被重写.那么这种方法用final来修饰 3.假设一个变量的值不希望 ...

  3. 最长连续序列(Longest Consecutive Sequence)

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  4. POJ 2249-Binomial Showdown(排列组合计数)

    Binomial Showdown Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18457   Accepted: 563 ...

  5. 用户'sa'登录失败(错误18456)解决方案图解

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://thenear.blog.51cto.com/4686262/865544 htt ...

  6. PHP中多维数组查找某个值是否存在的方法

    in_array — 检查数组中是否存在某个值,只是这个方法不能检查多维数组. 所以可以编写类似下面的递归方法来检查多维数组. function deep_in_array($value, $arra ...

  7. windows下搭建hadoopproject(一)

    这里是接着之前的一篇 <hadoop在windows下的环境搭建 >来的~~~ 一.安装文件准备 1:下载好hadoop-1.0.0.tar.gz, 下载地址是https://archiv ...

  8. node JS 微信开发

    JS-SDK 要点 微信测试号; 扫码登录;无需认证(只是名称统一为微信测试号)http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/logi ...

  9. Fedora 25/24/23 nVidia Drivers Install Guide

    https://www.if-not-true-then-false.com/2015/fedora-nvidia-guide/ search Most Popular Featured Linux ...

  10. iOS移动开发周报-第17期

    lhq iOS移动开发周报-第17期 前言 欢迎国内的iOS同行或技术作者向我提交周报线索,线索可以是新闻.教程.开发工具或开源项目,将相关文章的简介和链接在微博上发布并 @唐巧_boy 即可. [摘 ...