代码改变世界

ModelDriven

2019-04-20 20:20  阿方技术圈  阅读(24)  评论(0)    收藏  举报

用户在提交http请求的时候,一般有两种方式:一种是get方式,该方式用来获取查询相关信息,即向服务器索取数据。另一种方式是post方式,用来更新信息,即向服务器提交数据。

通常get方式向服务器查询信息时附带的信息较少,可以使用Servlet API来一一获取,而post方式提交的数据量大。struts2框架提供了ModelDriven接口,对于实现了该接口的Action来说,只需定义一个Model,struts2框架会自动地将用户提交的http信息赋值给相应的Model

ModelDriven接口使用案例

首先创建一个Web项目,配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>modelDrivenDemo</display-name>
  <welcome-file-list>
    <welcome-file>index.html</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>
  <filter>
   <!-- filter名称 -->
     <filter-name>struts2</filter-name>
     <!-- filter入口 -->
     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
      <init-param>
      <param-name>struts.i18n.encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
  </filter>
  
  <filter-mapping>
   <!-- filter名称 -->
     <filter-name>struts2</filter-name>
      <!-- 截获所有的URL -->
     <url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>

在src目录下创建一个com.struts.action包,在该包下创建一个LoginAction类

package com.struts.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.struts.model.UserModel;

public class LoginAction extends ActionSupport implements ModelDriven<UserModel>{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    //创建UserModel实例
    private UserModel user = new UserModel();
    
    //重载execute方法
    public String execute() throws Exception{
        ActionContext context = ActionContext.getContext();
        context.put("user", user);
        return SUCCESS;
    }

    //getter方法必须实现
    @Override
    public UserModel getModel() {
        // TODO Auto-generated method stub
        return user;
    }

}

在src目录下创建com.struts.model包,在该包下创建UserModel类

package com.struts.model;

public class UserModel {
    private String name;
    private String age;
    private String address;
    private String telephone;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getTelephone() {
        return telephone;
    }
    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }
    

}

在src下创建struts.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
  <package name="default" extends="struts-default" namespace="/">
    <action name="user" class="com.struts.action.LoginAction">
      <result name="success">/success.jsp</result>
    </action>
  </package>
</struts>

在WebContent目录下创建index.jsp和success.jsp页面

index.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
   <!-- 定义表单 -->
   <s:form action="user">
     <s:textfield label="Name" name="name"/>
     <s:textfield label="Age" name="age"/>
     <s:textfield label="Telephone" name="telephone"/>
     <s:textfield label="Address" name="address"/>
     <s:submit/>
   </s:form>
</center>
</body>
</html>

success.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
  <s:property value="#user.name"/><br/>
  <s:property value="#user.age"/><br/>
  <s:property value="#user.telephone"/><br/>
  <s:property value="#user.address"/><br/>
</center>
</body>
</html>

将该项目部署到tomcat服务器中进行测试

 

 

点击submit进行提交后显示success.jsp页面