springmvc小案例二

本文介绍了一个使用SpringMVC实现的员工信息添加功能示例,包括表单页面设计、SpringMVC配置文件设置、web.xml配置、Action类处理逻辑及实体类定义等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

添加页面------------------------------

<form action="${pageContext.request.contextPath }/empAction.action" method="post">
     <table border="2" align="center">
      <tr>
       <th>姓名</th>
       <td><input type="text" name="name"/></td>
      </tr>
      <tr>
       <th>性别</th>
       <td>
        <input type="radio" name="sex" value="男"/>男
        <input type="radio" name="sex" value="女"/>女
       </td>
      </tr>
      <tr>
       <th>入职时间</th>
       <td><input type="text" name="date" value="2016-05-14"/></td>
      </tr>
      <tr>
       <td colspan="2" align="center"><input type="submit"  value="提交"/></td>
      </tr>
     </table>
    </form>

-----------------------springmvc配置文件---------------------------

<?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:mvc="http://www.springframework.org/schema/mvc"
  
      xsi:schemaLocation="
 
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
       
      ">
         
    <bean name="/empAction.action" class="cn.gdpe.emp.EmpAction"></bean>
   
     <!-- 专用于jsp到jsp/html的转发控制器 -->
    <bean name="/testJsp.action" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
     <!-- 转发到真实视图名 -->
     <property name="viewName" value="/forwardTestJsp.jsp"/>
    </bean>  

 <!-- 注册Action
        class代表处理类的全路径
        name表示请求路径
     -->
  
    <bean id="userActionId" class="cn.gdpe.springmvc.UserAction"></bean>

 <!-- 映射器
        表示将bean标签的name属性当做URL请求
        可选
     
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>-->
    
    <!--SimpleUrlHandlerMapping将多个请求映射到同一个Action
        
      -->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/add.action">userActionId</prop>
                <prop key="/delete.action">userActionId</prop>
                <prop key="/update.action">userActionId</prop>
                <prop key="/select.action">userActionId</prop>
            </props>
        </property>
    </bean>
    
    <!-- 适配器
        表示用于寻找实现了controller接口的action类
        可选
     -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
    
    
    <!-- 视图器
        表示用于匹配modelAndView.setViewName("/springMvcFirst.jsp");中封装的路径
        可选
     -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/userjsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean> 
</beans>

--------------------------web.xml文件---------------------------

 <filter>
  <filter-name>characterEncoding</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>characterEncoding</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <servlet>
    <servlet-name>DispatcherServlet</servlet-name>
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>classpath:springmvc.xml</param-value>
         </init-param>
     </servlet>
     <servlet-mapping>
         <servlet-name>DispatcherServlet</servlet-name>
         <url-pattern>*.action</url-pattern>
     </servlet-mapping>

-------------------------------action类----------------------------------

public class EmpAction extends AbstractCommandController {
 public EmpAction(){
  this.setCommandClass(Emp.class);
  System.out.println("EmpAction()构造器");
 }
 /*如果不用注解的方式 继承AbstractCommandController 可以直接得到实体对象,并且为你封装好了数据  this.setCommandClass(Emp.class);这句代码 必须在构造器*/

 

 /*
  * object-->封装的实体类
  * bindException-->封装错误
  */
 @Override
 protected ModelAndView handle(HttpServletRequest request,
   HttpServletResponse response, Object object, BindException bindException)
   throws Exception {
  ModelAndView modelAndView=new ModelAndView();
  request.setAttribute("message", "增加员工");
  System.out.println("handle()方法");
  Emp emp=(Emp) object;
  request.setAttribute("emp", emp);
  modelAndView.setViewName("/successAdd.jsp");
  return modelAndView;
 }
 /*
  * 自定义类型转换器
  */

 

 @Override
 protected void initBinder(HttpServletRequest request,
   ServletRequestDataBinder binder) throws Exception {
  //参数一:将String类型转换为什么类型---》目标类型
  //参数二:自定义转换规则
  binder.registerCustomEditor(Date.class,
    new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
 }

-----------------------实体类------------------------

import java.util.Date;

 

public class Emp {
 public String name;
 public String sex;
 public Date date;
 public Date getDate() {
  return date;
 }
 public void setDate(Date date) {
  this.date = date;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getSex() {
  return sex;
 }
 public void setSex(String sex) {
  this.sex = sex;
 }
 @Override
 public String toString() {
  return "Emp [name=" + name + ", sex=" + sex + ", date=" + date.toLocaleString() + "]";
 }
 
 
}

-----------------转向页面----------------------

 <body>
    ${message }
    ${requestScope.emp.name }
    ${requestScope.emp.sex }
    <fmt:formatDate value="${requestScope.emp.date }" type="date" dateStyle="default"/>
  </body>

 

转载于:https://my.oschina.net/chenliyong/blog/675420

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值